Re: SQLAlchemy: How to do Table Reflection and MySQL?
On Oct 20, 6:24 pm, Nick Sabalausky wrote: > Hi, I'm fairly new to Python, and I'm trying to figure out how to use > SQLAlchemy to connect to a MySQL DB and use table reflection to set up > SQLAlchemy's tables. But the SQLAlchemy documentation is gigantic and > frankly kinda making my head spin, so I'm having trouble even finding > any information on how to use its table reflection, mostly just that it > exists and *can* be done, but not so much "how". My web searching has > just been turning up examples of SQLite and manually describing the > tables in Python and having SQLAlchemy create the tables, which isn't > what I'm looking for. > > Is there a simple way to do this somehow? To just connect to a MySQL DB > and use table reflection? i'm not brave enough to dig too deeply into SQLAlchemy, but maybe this will help? : http://kashififtikhar.blogspot.com/2010/07/using-sqlalchemy-reflection-with-pylons.html that came up from googling "sqlalchemy table reflection tutorial". -- http://mail.python.org/mailman/listinfo/python-list
Re: Trying to make a basic Python score counter in a game... will not count.
On Dec 16, 12:38 pm, tbg wrote: > Nice, will have to try it out... if you're interested in learning Python and/or game programming in Python, you might want to take a look at http://inventwithpython.com/ . HTH, Don -- http://mail.python.org/mailman/listinfo/python-list
Re: Searching through two logfiles in parallel?
i don't think in iterators (yet), so this is a bit wordy.
same basic idea, though: for each message (set of parameters), build a
list of transactions consisting of matching send/receive times.
mildly tested:
from datetime import datetime, timedelta
sendData = '''\
05:00:06 Message sent - Value A: 5.6, Value B: 6.2, Value C: 9.9
05:00:08 Message sent - Value A: 3.3, Value B: 4.3, Value C: 2.3
05:00:10 Message sent - Value A: 3.0, Value B: 0.4, Value C: 5.4
#orphan
05:00:14 Message sent - Value A: 1.0, Value B: 0.4, Value C: 5.4
07:00:14 Message sent - Value A: 1.0, Value B: 0.4, Value C: 5.4
'''
receiveData = '''\
05:00:09 Message received - Value A: 5.6, Value B: 6.2, Value C:
9.9
05:00:12 Message received - Value A: 3.3, Value B: 4.3, Value C:
2.3
05:00:15 Message received - Value A: 1.0, Value B: 0.4, Value C:
5.4
07:00:18 Message received - Value A: 1.0, Value B: 0.4, Value C:
5.4
07:00:30 Message received - Value A: 1.0, Value B: 0.4, Value C:
5.4 #orphan
07:00:30 Message received - Value A: 17.0, Value B: 0.4, Value C:
5.4 #orphan
'''
def parse(line):
timestamp, rest = line.split(' Message ')
action, params = rest.split(' - ' )
params = params.split('#')[0]
return timestamp.strip(), params.strip()
def isMatch(sendTime,receiveTime,maxDelta):
if sendTime is None:
return False
sendDT = datetime.strptime(sendTime,'%H:%M:%S')
receiveDT = datetime.strptime(receiveTime,'%H:%M:%S')
return receiveDT - sendDT <= maxDelta
results = {}
for line in sendData.split('\n'):
if not line.strip():
continue
timestamp, params = parse(line)
if params not in results:
results[params] = [{'sendTime': timestamp, 'receiveTime':
None}]
else:
results[params].append({'sendTime': timestamp, 'receiveTime':
None})
for line in receiveData.split('\n'):
if not line.strip():
continue
timestamp, params = parse(line)
if params not in results:
results[params] = [{'sendTime': None, 'receiveTime':
timestamp}]
else:
for tranNum, transaction in enumerate(results[params]):
if
isMatch(transaction['sendTime'],timestamp,timedelta(seconds=5)):
results[params][tranNum]['receiveTime'] = timestamp
break
else:
results[params].append({'sendTime': None, 'receiveTime':
timestamp})
for params in sorted(results):
print params
for transaction in results[params]:
print '\t%s' % transaction
>>> RESTART
>>>
Value A: 1.0, Value B: 0.4, Value C: 5.4
{'sendTime': '05:00:14', 'receiveTime': '05:00:15'}
{'sendTime': '07:00:14', 'receiveTime': '07:00:18'}
{'sendTime': None, 'receiveTime': '07:00:30'}
Value A: 17.0, Value B: 0.4, Value C: 5.4
{'sendTime': None, 'receiveTime': '07:00:30'}
Value A: 3.0, Value B: 0.4, Value C: 5.4
{'sendTime': '05:00:10', 'receiveTime': None}
Value A: 3.3, Value B: 4.3, Value C: 2.3
{'sendTime': '05:00:08', 'receiveTime': '05:00:12'}
Value A: 5.6, Value B: 6.2, Value C: 9.9
{'sendTime': '05:00:06', 'receiveTime': '05:00:09'}
>>>
HTH,
Don
--
http://mail.python.org/mailman/listinfo/python-list
Re: which book?
On May 10, 4:58 am, [email protected] wrote: > On Wednesday, May 9, 2012 7:13:54 AM UTC-7, Miki Tebeka wrote: > > > I am going to learn python for some plot issues. which book or sources, > > > do you recommend please? > > The tutorial is pretty good if you already know how to program. > > I also heard a lot of good things on "Python Essential Reference". > > Thanks. > Could you please pass the line for tutorial? i believe that would be the tutorial at http://docs.python.org/tutorial/ . -- http://mail.python.org/mailman/listinfo/python-list
Re: split long string in two code lines
print "this" \ " is" \ " a" \ " test" \ >>> RESTART >>> this is a test -- http://mail.python.org/mailman/listinfo/python-list
Re: Methods on file-like objects can only used once on one object?
On Aug 23, 9:21 am, Yingjie Lin wrote:
> Hi Python users,
>
> I just realize that my post yesterday shouldn't be specifically for
> mechanize. It should be a general question for file-like objects.
>
> >>> f = open('my_file.txt')
> >>> print f.readlines()
>
> ( prints a list of strings>>> print f.readlines()
>
> []
>
> There are quite a few methods for file-like objects that can only be used
> once on one object. If I prefer to use some of these methods on one object,
> one after another, like:
>
> f.readlines()
> f.read()
> ...
>
> What should I do? Thank you.
>
> - Yingjie
Each of those calls consumes the entire file, leaving the file pointer
at end-of-file. to reset the file pointer back to the beginning of the
file and enable re-reading, use f.seek(0) .
--
http://mail.python.org/mailman/listinfo/python-list
Re: Running Python programmes
Maybe you're inadvertently running Python with either the '-i' switch or with the PYTHONINSPECT environment variable set? When you do that, your script will launch an interactive prompt after it completes. C:\Python27>echo print "hello" > hello.py C:\Python27>python hello.py hello C:\Python27>python -i hello.py hello >>> >>> ^Z C:\Python27>set PYTHONINSPECT=1 C:\Python27>python hello.py hello >>> >>> ^Z -- https://mail.python.org/mailman/listinfo/python-list
Re: test for list equality
On Dec 15, 11:59 am, Miki Tebeka wrote: > > My sort issue... as in this doesn't work > > >>> if x.sort == y.sort: > > You're missing the () to make it a function call. > Also list.sort() returns none, it mutates the original list. > You can either > sorted(x) == sorted(y) > or > set(x) == set(y) I'm pretty sure we don't want to use set() since it throws away duplicates: >>> x = [1,2,3,4] >>> y = [1,1,2,2,3,3,4] >>> sorted(x) == sorted(y) False >>> set(x) == set(y) True -- http://mail.python.org/mailman/listinfo/python-list
Re: Issue with my code
On Feb 5, 2:19 pm, maiden129 wrote:
> How to reverse the two loops?
>
s=input("Enter a string, eg(4856w23874): ")
checkS=['0','1','2','3','4','5','6','7','8','9']
for digit in checkS:
t = s.count(digit)
if t == 0:
pass
elif t == 1:
print(digit,"occurs 1 time.")
else:
print(digit, "occurs", t,"times.")
>>>
Enter a string, eg(4856w23874): 23493049weee2039412367
0 occurs 2 times.
1 occurs 1 time.
2 occurs 3 times.
3 occurs 4 times.
4 occurs 3 times.
6 occurs 1 time.
7 occurs 1 time.
9 occurs 3 times.
>>>
HTH,
Don
--
http://mail.python.org/mailman/listinfo/python-list
Re: Issue with my code
On Feb 5, 4:05 pm, marduk wrote:
>
> Although that implementation also scans the string 10 times (s.count()),
> which may not be as efficient (although it is happening in C, so perhaps
> not).
>
> A better solution involves only scanning the string once.
agreed. i was specifically showing how to reverse the loop.
using the much-better-suited Counter class:
from collections import Counter
s=input("Enter a string, eg(4856w23874): ")
checkS=['0','1','2','3','4','5','6','7','8','9']
cnt = Counter()
for char in s:
cnt[char] += 1
for char, tally in sorted(cnt.items()):
if char in checkS and tally > 0:
if tally == 1:
print(char,"occurs 1 time.")
else:
print(char, "occurs", tally,"times.")
>>>
Enter a string, eg(4856w23874): 192398209asdfbc12903348955
0 occurs 2 times.
1 occurs 2 times.
2 occurs 3 times.
3 occurs 3 times.
4 occurs 1 time.
5 occurs 2 times.
8 occurs 2 times.
9 occurs 5 times.
>>>
HTH,
Don
--
http://mail.python.org/mailman/listinfo/python-list
Re: Python log parser
i know literally nothing about syslogs, but a google search for "python syslog parser" shows that some people have had success using the pyparsing module: http://www.j-schmitz.net/blog/how-to-parse-a-syslog-logfile-in-python https://gist.github.com/leandrosilva/3651640 hth, Don -- http://mail.python.org/mailman/listinfo/python-list
Re: a couple of things I don't understand wrt lists
On Apr 17, 5:25 am, aaB wrote: > > - the "complement" thing: > I haven't yet tried to reproduce this, but I will, and I will post back if I > see > this happening again, this time with a real log of python's interactive > console, > or a complete script which people can use. > That was happening when you incorrectly used bit as an index back into bitpattern. When you do that, the behavior actually changes depending on the value of bitpattern: a bitpattern that starts with [1, 0, ...] will yield its complement: >>> bitpattern = [1, 0, 0, 1, 1, 0, 1] >>> for bit in bitpattern: print 'bitpattern[%s] : %s' % (bit, bitpattern[bit]) bitpattern[1] : 0 bitpattern[0] : 1 bitpattern[0] : 1 bitpattern[1] : 0 bitpattern[1] : 0 bitpattern[0] : 1 bitpattern[1] : 0 >>> while a bitpattern that starts with [0, 1, ...] will yield the expected results: >>> bitpattern = [0, 1, 0, 1, 1, 0, 1] >>> for bit in bitpattern: print 'bitpattern[%s] : %s' % (bit, bitpattern[bit]) bitpattern[0] : 0 bitpattern[1] : 1 bitpattern[0] : 0 bitpattern[1] : 1 bitpattern[1] : 1 bitpattern[0] : 0 bitpattern[1] : 1 >>> HTH, Don -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing soap result
On Apr 17, 8:50 am, Ombongi Moraa Fe
wrote:
> how do I use xml.etree.ElementTree to print the parameters address and
> deliveryStatus? Or is there a better python method?
>
I'm sure there are prettier ways to do this, but you can use XPath
syntax to find all of your ns1:result nodes and loop through them:
>>> import xml.etree.ElementTree as ET
>>> myXML = '''\
http://schemas.xmlsoap.org/soap/
envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
http://www.csapi.org/schema/parlayx/sms/send/v2_2/
local">
254727
DeliveredToNetwork
'''
>>> myNamespaces=dict(ns1="http://www.csapi.org/schema/parlayx/sms/send/v2_2/local",soapenv="http://schemas.xmlsoap.org/soap/envelope/";)
>>> root = ET.fromstring(myXML)
>>> for result in root.findall('.//ns1:result',namespaces=myNamespaces):
address = result.find('address').text
deliveryStatus = result.find('deliveryStatus').text
print "address: %s, deliveryStatus: %s" % (address,deliveryStatus)
address: 254727, deliveryStatus: DeliveredToNetwork
>>>
HTH,
Don
--
http://mail.python.org/mailman/listinfo/python-list
Re: Parsing soap result
On Apr 17, 1:05 pm, Christian Heimes wrote:
> Am 17.04.2013 19:55, schrieb darnold:
>
> > On Apr 17, 8:50 am, Ombongi Moraa Fe
> > wrote:
>
> >> how do I use xml.etree.ElementTree to print the parameters address and
> >> deliveryStatus? Or is there a better python method?
>
> > I'm sure there are prettier ways to do this, but you can use XPath
> > syntax to find all of your ns1:result nodes and loop through them:
>
> You want all {http://www.csapi.org/schema/parlayx/sms/send/v2_2/
> local}result tags. The prefix isn't fixed.
I'm sorry, but I'm not understanding the difference.
By specifying:
>>> myNamespaces=dict(ns1="http://www.csapi.org/schema/parlayx/sms/send/v2_2/local";)
Isn't this:
>>> for result in root.findall('.//ns1:result',namespaces=myNamespaces):
equivalent to:
>>> for result in
>>> root.findall('.//{http://www.csapi.org/schema/parlayx/sms/send/v2_2/local}result'):
?
Or am I misunderstanding? Is there a namespace-agnostic way of doing
this?
Admittedly, I haven't used ElementTree or XPath much prior to toying
with them to (attempt to) answer the OP's question.
Thanks for your patience,
Don
--
http://mail.python.org/mailman/listinfo/python-list
Re: Howw to prevent the duplication of any value in a column within a CSV file (python)
potential_passengers = ['bob','john','sue','wendy','chris','bob','jen','wendy']
accepted_passengers = set()
for name in potential_passengers:
print('checking on {}...'.format(name))
if name not in accepted_passengers:
accepted_passengers.add(name)
print('welcome aboard, {}!'.format(name))
else:
print('i am sorry, we have already accepted a {}.'.format(name))
print()
HTH,
Don
--
https://mail.python.org/mailman/listinfo/python-list
Re: Looking for direction
I recommend getting your hands on "Automate The Boring Stuff With Python" from no starch press: http://www.nostarch.com/automatestuff I've not read it in its entirety, but it's very beginner-friendly and is targeted at just the sort of processing you appear to be doing. HTH, Don -- https://mail.python.org/mailman/listinfo/python-list
Re: How may I learn Python Web Frameworks
you'll find a very extensive Flask tutorial at http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world . -- https://mail.python.org/mailman/listinfo/python-list
