How to "style" a text based hangman game - python

My question is simple, the hangman game looks like this:
I'm doing the indentation in a way I don't think is very good.
I just have var = "\t" and add it at the begging of every print, this seem impractical and hard to maintain.
Would you have it any other way? How is this generally managed?
Thanks in advance!

problem:
print('\tthis')
print('\tis')
print('\tan')
print('\texample')
solution = abstraction!:
def echo(string, indent=1): # name it whatever you want, e.g. p
print('\t'*indent + string)
echo('this')
echo('is')
echo('an')
echo('abstraction')
better solution = templates:
template = """
{partial}
______
{hangman}
|_________
Your points so far: {points}
You've entered (wrong): {wrong}
Choose a letter:
"""
print(
template.format(
partial=...,
hangman='\n'.join('|'+line for line in hangmanAscii(wrong=2).splitlines()),
points=...,
wrong=', '.join(...)
)
)

Templates aren't just for HTML.

A quick solution to your immediate problem is a helper function:
def put(text): print "\t" + text
For more interesting stuff, there is a library for terminal-based "graphical"-like applications. It is called "curses", and is available in python too: http://docs.python.org/library/curses.html. There is a tutorial here http://docs.python.org/howto/curses.html, and you can find more online.
Curses makes it possible to do more interesting text-based UI without too much effort. To just replicate what you have, you would probably create a window with the correct size and position (using newwin), and then print text to it without any indent (using addstr). Curses offers a lot of options and several modes of operation. It's not quick to learn, so only makes sense if you plan to do more with terminal based applications.

I'd probably fix the typo in "You word looks like this". Then look at printf style formatting, as described here.

Related

Python - How to fix CWE-117: Improper Output Neutralization for Logs

I have tried using clean = message.replace('\n', '').replace('\r', '').
But it still fails the veracode scan.
Is there any better way to do it?
Also I read about ESAPI Python but not getting any good documentation to fix this issue.
ESAPI Python unfortunately is an extremely long dead project. I don't have any Python alternatives, but the library hasn't had a commit since August 9, 2010.
I'm not familiar with Python web frameworks, but I do teach regularly about secure programming.
Every time you make a function call, you need to ask yourself the question:
"Am I passing this off of to an interpreter?"
If the answer is "yes," then you need to encode to ensure that THAT function is going to treat the input purely as data and not executable code.
In this particular case, if your logging has the potential to be interpreted by a browser (which is common) you'd also want to encode for HTML. However if your environment never uses html-based logging and is always raw text, this could be considered "mitigated" or a "false positive" depending on how your company has agreed to those terms.
Also, I like the practice of wrapping any user input with a unique delimiter. You can use square brackets, but I like strong visual cues in my logs.
Picture: [USERACTION] + userId + ' ►' + encode(userInput) + '◀'
Or even:
[USERACTION] + userId + ' ( ͡ʘ ͜ʖ ͡ʘ)' + encode(userInput) + '<:::::[]=¤ (▀̿̿Ĺ̯̿̿▀̿ ̿)'
Anything that helps make it obvious that someone has tampered with your logs is good. The louder the better!

pylatex: how to apply {\centering <x> } around graphics and or text?

I am trying to create the following (type of) code with PyLatex:
{
\centering
\includegraphics{image.png}
\includegraphics{image.png}
}
I am not able to 'group' the \centering command with the curly brackets around the \includegraphics; if I define a new Environment, I always end up with \begin and \end which add spacing.
So, do you know how I can nicely add the curly brackets with the \centering command around a snippet of code like \includegrahics?
To me, the most elegant solution would be something like:
with doc.centering():
doc.append('Some text.')
append(StandAloneGraphic(image))
append(StandAloneGraphic(image2))
How can I define such '''with doc.centering()''' command? Of course, I can hard code this every time, but I want my code to be readable so it would be nice if I could write a nice function for this problem.
I posted this question earlier om Stack Exchange (https://tex.stackexchange.com/questions/490429/pylatex-how-to-apply-centering-x-around-graphic/490442), but over there I got the advice to post here because it is more of a Python programming problem than a PyLatex challenge.
Update: I would like to put teh code below in a nice with.group(): statement:
import pylatex as pl
doc = pl.Document()
doc.preamble.append(pl.Package('showframe'))
doc.append(pl.NoEscape('{'))
doc.append(pl.Command('centering'))
doc.append(pl.StandAloneGraphic('example-image',image_options='width=5cm'))
doc.append(pl.Command('par'))
doc.append(pl.NoEscape('}'))
doc.generate_tex('pylatexdemo')
This piece of code creates the correct output, but is not really readable. I would imagine such solution (but I don't know how to program it); the definition of the group-function is incorrect. My question is: how to implement such group-function?
import pylatex as pl
doc = pl.Document()
doc.preamble.append(pl.Package('showframe'))
def group(content):
doc.append(pl.NoEscape('{'))
doc.append(content)
doc.append(pl.Command('par'))
doc.append(pl.NoEscape('}'))
with doc.group():
doc.append(pl.Command('centering'))
doc.append(pl.StandAloneGraphic('example-image',image_options='width=5cm'))
doc.generate_tex('pylatexdemo')

Python: Matching & Stripping port number from socket data

I have data coming in to a python server via a socket. Within this data is the string '<port>80</port>' or which ever port is being used.
I wish to extract the port number into a variable. The data coming in is not XML, I just used the tag approach to identifying data for future XML use if needed. I do not wish to use an XML python library, but simply use something like regexp and strings.
What would you recommend is the best way to match and strip this data?
I am currently using this code with no luck:
p = re.compile('<port>\w</port>')
m = p.search(data)
print m
Thank you :)
Regex can't parse XML and shouldn't be used to parse fake XML. You should do one of
Use a serialization method that is nicer to work with to start with, such as JSON or an ini file with the ConfigParser module.
Really use XML and not something that just sort of looks like XML and really parse it with something like lxml.etree.
Just store the number in a file if this is the entirety of your configuration. This solution isn't really easier than just using JSON or something, but it's better than the current one.
Implementing a bad solution now for future needs that you have no way of defining or accurately predicting is always a bad approach. You will be kept busy enough trying to write and maintain software now that there is no good reason to try to satisfy unknown future needs. I have never seen a case where "I'll put this in for later" has led to less headache later on, especially when I put it in by doing something completely wrong. YAGNI!
As to what's wrong with your snippet other than using an entirely wrong approach, angled brackets have a meaning in regex.
Though Mike Graham is correct, using regex for xml is not 'recommended', the following will work:
(I have defined searchType as 'd' for numerals)
searchStr = 'port'
if searchType == 'd':
retPattern = '(<%s>)(\d+)(</%s>)'
else:
retPattern = '(<%s>)(.+?)(</%s>)'
searchPattern = re.compile(retPattern % (searchStr, searchStr))
found = searchPattern.search(searchStr)
retVal = found.group(2)
(note the complete lack of error checking, that is left as an exercise for the user)

How to match search strings to content in python

Usually when we search, we have a list of stories, we provide a search string, and expect back a list of results where the given search strings matches the story.
What I am looking to do, is the opposite. Give a list of search strings, and one story and find out which search strings match to that story.
Now this could be done with re but the case here is i wanna use complex search queries as supported by solr. Full details of the query syntax here. Note: i wont use boost.
Basically i want to get some pointers for the doesitmatch function in the sample code below.
def doesitmatch(contents, searchstring):
"""
returns result of searching contents for searchstring (True or False)
"""
???????
???????
story = "big chunk of story 200 to 1000 words long"
searchstrings = ['sajal' , 'sajal AND "is a jerk"' , 'sajal kayan' , 'sajal AND (kayan OR bangkok OR Thailand OR ( webmaster AND python))' , 'bangkok']
matches = [[searchstr] for searchstr in searchstrings if doesitmatch(story, searchstr) ]
Edit: Additionally would also be interested to know if any module exists to convert lucene query like below into regex:
sajal AND (kayan OR bangkok OR Thailand OR ( webmaster AND python) OR "is a jerk")
After extensive googling, i realized what i am looking to do is a Boolean search.
Found the code that makes regex boolean aware : http://code.activestate.com/recipes/252526/
Issue looks solved for now.
Probably slow, but easy solution:
Make a query on the story plus each string to the search engine. If it returns anything, then it matches.
Otherwise you need to implement the search syntax yourself. If that includes things like "title:" and stuff this can be rather complex. If it's only the AND and OR from your example, then it's a recursive function that isn't too hairy.
Some time ago I looked for a python implementaion of lucene and I came accross of Woosh which is a pure python text-based research engine. Maybe it will statisfy your needs.
You can also try pyLucene, but i did'nt investigate this one.
Here's a suggestion in pseudocode. I'm assuming you store a story identifier with the search terms in the index, so that you can retrieve it with the search results.
def search_strings_matching(story_id_to_match, search_strings):
result = set()
for s in search_strings:
result_story_ids = query_index(s) # query_index returns an id iterable
if story_id_to_match in result_story_ids:
result.add(s)
return result
This is probably less interesting to you now, since you've already solved your problem, but what you're describing sounds like Prospective Search, which is what you call it when you have the query first and you want to match it against documents as they come along.
Lucene's MemoryIndex is a class that was designed specifically for something like this, and in your case it might be efficient enough to run many queries against a single document.
This has nothing to do with Python, though. You'd probably be better off writing something like this in java.
If you are writing Python on AppEngine, you can use the AppEngine Prospective Search Service to achieve exactly what you are trying to do here. See: http://code.google.com/appengine/docs/python/prospectivesearch/overview.html

How can I use Microsoft Word's spelling/grammar checker programmatically?

I want to process a medium to large number of text snippets using a spelling/grammar checker to get a rough approximation and ranking of their "quality." Speed is not really of concern either, so I think the easiest way is to write a script that passes off the snippets to Microsoft Word (2007) and runs its spelling and grammar checker on them.
Is there a way to do this from a script (specifically, Python)? What is a good resource for learning about controlling Word programmatically?
If not, I suppose I can try something from Open Source Grammar Checker (SO).
Update
In response to Chris' answer, is there at least a way to a) open a file (containing the snippet(s)), b) run a VBA script from inside Word that calls the spelling and grammar checker, and c) return some indication of the "score" of the snippet(s)?
Update 2
I've added an answer which seems to work, but if anyone has other suggestions I'll keep this question open for some time.
It took some digging, but I think I found a useful solution. Following the advice at http://www.nabble.com/Edit-a-Word-document-programmatically-td19974320.html I'm using the win32com module (if the SourceForge link doesn't work, according to this Stack Overflow answer you can use pip to get the module), which allows access to Word's COM objects. The following code demonstrates this nicely:
import win32com.client, os
wdDoNotSaveChanges = 0
path = os.path.abspath('snippet.txt')
snippet = 'Jon Skeet lieks ponies. I can haz reputashunz? '
snippet += 'This is a correct sentence.'
file = open(path, 'w')
file.write(snippet)
file.close()
app = win32com.client.gencache.EnsureDispatch('Word.Application')
doc = app.Documents.Open(path)
print "Grammar: %d" % (doc.GrammaticalErrors.Count,)
print "Spelling: %d" % (doc.SpellingErrors.Count,)
app.Quit(wdDoNotSaveChanges)
which produces
Grammar: 2
Spelling: 3
which match the results when invoking the check manually from Word.

Categories

Resources