Code Bowling on "Hello World"? [closed]
You asked for it. Python:
# Copyright (c) 1999 - 2010
# Large Company, Inc. ("THE COMPANY")
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COMPANY AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COMPANY OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
"""This program outputs an enthusiastic "Hello World" in english.
"""
# FIXME: Where are the unit tests for this? QA called and says we
# can't ship this without tests. Also, while writing documentation
# may be boring, you can't expect everyone to understand all this!
# Go ahead and write some docstrings! -- O.D. 2004/7/22
class Expression(object):
def get_value(self, **kwargs):
"""get_value returns the value of this Expression.
Any keyword arguments that the method receives should
be passed on to the get_value methods of possibly called
subexpressions, even if this method does not handle
them.
This method must be reimplemented by the subclass."""
raise NotImplementedError
class Word(Expression):
def __init__(self, value):
self.value = value
def get_value(self, **kwargs):
return self.value
class Sentence(Expression):
def __init__(self, expressions, punctuation = "."):
self.expressions = list(expressions)
self.punctuation = punctuation
def get_value(self, separator = " ", **kwargs):
mainpart = separator.join(
subexpression.get_value(separator = separator, **kwargs)
for subexpression in self.expressions
)
if len(mainpart) > 0:
capitalized = mainpart[0].upper() + mainpart[1:]
else:
capitalized = ""
# FIXME: We're hardcoding "" here. Should we be prepared for
# languages that require a separator before the punctuation mark?
# NB: A workaround for now would be adding an empty word
return "".join((capitalized, self.punctuation))
class Hello(Word):
# FIXME: We should be prepared for languages where "hello" is
# represented by more than one word.
hello_by_language = {"en": "hello", "de": "hallo"}
def __init__(self, language = "en"):
super(Hello, self).__init__(self.hello_by_language[language])
class World(Word):
# FIXME: We should be prepared for languages where "world" is
# represented by more than one word.
world_by_language = {"en": "world", "de": "Welt"}
def __init__(self, language = "en"):
super(World, self).__init__(self.world_by_language[language])
class HelloWorld(Sentence):
def __init__(self, punctuation, language):
hello = Hello(language)
world = World(language)
super(HelloWorld, self).__init__([hello, world], punctuation)
class EnthusiasticHelloWorld(HelloWorld):
def __init__(self, language):
# FIXME: We should be prepared for languages where enthusiasm
# is not expressed with an exclamation mark.
super(EnthusiasticHelloWorld, self).__init__("!", language)
def main():
english_enthusiastic_hello_world = EnthusiasticHelloWorld("en")
print english_enthusiastic_hello_world.get_value()
if __name__ == "__main__":
main()
Can someone help me speed up my program. Python is so slow even to run just one line!
python -c '[__import__("os").write(1,__import__("urllib2").urlopen("http://stackoverflow.com/questions/2052137").read()[x+__import__("urllib2").urlopen("http://stackoverflow.com/questions/2052137").read().find("Hello World")]) for x,_ in enumerate("Hello World")]'