Spoiler alert: This was encountered while working on MIT OCW 6.000
problem set 4.
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/assignments/ps4.py
My function returns a list as it should:
#
I'm rewriting a bunch of my bash scripts to get some python practice.
There are some instances where python doesn't have the built-in text
processing that I need, so I'm using subprocess.call. How can I pass
a python variable to these subprocesses? For example:
mydir = "/usr/local/bin"
subproces
What am I doing wrong here?
>>> from subprocess import Popen, PIPE
>>> cmd = 'ls -l'
>>> p = Popen(cmd, stdout=PIPE, stderr=PIPE)
Traceback (most recent call last):
File "", line 1, in ?
File "/usr/lib64/python2.4/subprocess.py", line 550, in __init__
errread, errwrite)
File "/usr/lib64/
Is there an easy way to find the target of a symbolic link on a Linux
system with python? I'm looking for something like the output of the
bash command: "ls -l"
___
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http:/
I rewrote a bash script that gathers data from a bunch of text files.
The script uses grep, sed, and awk to parse the data and takes around
5.5 seconds to run. After rewriting the script in python, it now
takes a little over 0.2 seconds to run. Thanks to those of you who
helped me with some quest
> Can we see a version of your script?
How about just a couple snippets...there's no proprietary data but I
want to be on the safe side. The original script consisted of a bunch
of functions similar to the one below. When I first wrote this I
didn't know how to use sed very well, so I used varia
Hi folks:
I'm trying to define a short function that will import yaml data into
a python dictionary. I am able to do this by dumping my data into a
temporary file and then importing it with yaml.load. I would like to
see if I can eliminate the temporary file and import the data
directly.
This w
10 at 11:08 AM, Sean Carolan wrote:
>>
>> Hi folks:
>>
>> I'm trying to define a short function that will import yaml data into
>> a python dictionary. I am able to do this by dumping my data into a
>> temporary file and then importing it with yaml.loa
Maybe one of you can help with this. I've got some data that I
grabbed from a cobbler *.json file using simplejson.load(). Heres
what the data looks like:
In [120]: print mydata.keys()
['comment', 'kickstart', 'name_servers_search', 'ks_meta',
'kernel_options_post', 'image', 'redhat_management_k
> I have a database with a table called "systems" that contains an
> auto-increment id field, as well as fields for each of the keys in
> mydata.keys(). But I can't seem to get the syntax to import
> mydata.values() into the table. I think the problem may be that some
> of the items in my list ar
I've got a csv file that contains two data fields, the short name of a
month and an integer. I'm experimenting with pylab and ipython to get
a feel for how pylab works. I'm able to generate a bar graph from my
data, but there are two problems with it:
1. I don't want "2011" appended to the mont
> I can also advice you to try the matplotlib mailing list: people will there
> will be much more knowledgeable wrt to matplotlib and pylab than on this
> mailing list:
> http://sourceforge.net/mailarchive/forum.php?forum_name=matplotlib-users
Thanks for all the info; I managed to create a grap
I'm practicing manipulating data with a text file. I'm trying to use
shlex.split to break up each line, which works great until it gets to
the first apostrophe:
fin = open('huckfinn.txt')
startstring = 'START OF THIS PROJECT'
for line in fin:
print line
words = shlex.split(line)
This is
>>> I am new to programming and to Python. I've been using Python with IDLE
>>> on Windows Vista for a few weeks now.
>>> (And I'm loving it!) However, I'm thinking about switching to Ubuntu
>>> 10.10. If I download Ubuntu, will I still be able to use the
>>> IDLE environment? I am really quite
I have a function that accepts four arguments, namely startmonth,
startyear, endmonth, and endyear. For example:
startmonth = 8
startyear = 2009
endmonth = 1
endyear = 2010
What would be the most straightforward way to create a list of
year/month pairs from start to end? I want to end up with a
> This sounds somewhat like homework. If it is, that's fine, mention it,
> and we will help you. But we won't do your homework for you, so keep
> that in mind.
A reasonable assumption but this is actually going in a cgi tool that
I'm using at work. The input comes from pull-down menus on a web
pa
> As far as I can tell from quickly going through documentation, no. At
> least, not with a quick and easy function. datetime can represent the
> dates just fine, and you can add days to that until you hit your end
> date, but adding months is harder. timedelta can't represent a month,
> which make
ipt that monitors things like load average, memory, disk
space, etc. Hint: you can get a *lot* of useful info from the /proc
directory, for example, /proc/meminfo, /proc/loadavg, etc.
Here's a quickie that I built for a client, it watches the 15 minute
load average.
#!/usr/bin/env python
Maybe someone can help with this. I have a function that takes a
single file as an argument and outputs a tuple with each line of the
file as a string element. This is part of a script that is intended
to concatenate lines in files, and output them to a different file.
This is as far as I've gott
On Tue, Mar 1, 2011 at 11:55 AM, Sean Carolan wrote:
> Maybe someone can help with this. I have a function that takes a
> single file as an argument and outputs a tuple with each line of the
> file as a string element. This is part of a script that is intended
> to concatenate li
> I saw in your follow-up that you went straight for vars(). I really
> don't think that's what you wish to use. Get rid of vars(), he had
> to go to jail. Don't go visit vars() again for at least two months,
> then maybe he'll be out on probation.
Thanks Martin and Hugo. As you can tell I'm n
> My advice would be to go read up on the zip() function and the
> str.join() function. Then, if you are using python 2.x, go find
> itertools.izip. It does the same thing as zip but it's more memory
> efficient. With those two you can do it in about two lines or so (and
> maybe a few for set up an
> Another way is:
>
> zip(*map(open, myfiles))
>> Then your loop looks like:
>>
>> for i in zip([ cleanedup(filename) for filename in myfiles ])
Thanks, Steven! I knew there was a way to do this with just a few
lines. I will read up some more on list expansion and the map
built-in.
I'm not sure how to do this. I'm reading lines in from a text file.
When I reach the string "notes:", I want to assign the remainder of
the text file to a single variable (line breaks and all):
text
moretext
moretext
notes:
This is the stuff I want in my variable.
And this line should be included
> So right now my code looks something like this:
>
> for line in open('myfile','r'):
> if line.startswith('notes'):
> ## Assign rest of file to variable
>
> Is there an easy way to do this? Or do I need to read the entire file
> as a string first and carve it up from there instead?
I ended
>> if line.startswith('notes'):
>> break
>> notes = open('myfile','r').read().split(notes:\n')[1]
>
> The first two lines are redundant you only need the last one.
I should have clarified, the "if line.startswith" part was used to
break out of the previous for loop, which was used to import the
In bash you can do this to see if a process is running:
[scarolan@kurobox:~/bin]$ kill -0 24275
[scarolan@kurobox:~/bin]$ echo $?
0
Is there a python equivalent? I tried using os.kill() but did not see
any way to capture the output.
___
Tutor maillist
I've got a function that builds a bar graph. We are 95% happy with
it, but the x-axis tick labels are a little bit skewed to the right.
Anyone know how to skew the labels over by 10 or 20 pixels? Here's
the code that generates my xticks:
xticks(range(len(builds)), users, rotation=30)
___
> I've got a function that builds a bar graph. We are 95% happy with
> it, but the x-axis tick labels are a little bit skewed to the right.
> Anyone know how to skew the labels over by 10 or 20 pixels? Here's
> the code that generates my xticks:
>
> xticks(range(len(builds)), users, rotation=30)
I'm working on a simple python web app that generates graphs, because
managers love graphs. I've got it about 90% done, but I'm having
trouble getting labels onto my stacked graph. In the matplotlib
documentation there is a nice example showing how to create the
legend. Note how the variables p1
> Unfortunately my graph is generated dynamically. How can I create my
> legend when my 'bar' objects have no names to refer to?
I also noticed another issue with my stacked bar graph; the total
height of the bar is the size of the largest number of the dataset,
and not the total of all the indivi
> Not me, but I notice there is a gmane newsfeed for matplotlib:
>
> gmane.comp.python.matplotlib.general
>
> Probably worth posting questions there.
Thank you, I will inquire on the newsfeed.
___
Tutor maillist - Tutor@python.org
To unsubscribe or cha
> Unfortunately my graph is generated dynamically. How can I create my
> legend when my 'bar' objects have no names to refer to?
>
> for admin in bd:
> bar(ind, bd[admin], width, color=colordict[admin])
> xticks(ind+width/2., datenames)
> legend()
> grid('on')
> outfile = 'tes
> I also noticed another issue with my stacked bar graph; the total
> height of the bar is the size of the largest number of the dataset,
> and not the total of all the individual items. Anyone matplotlib
> experts out there who can weigh in?
I figured out what was going on here; the bars were al
I'm trying to parse some XML data (Book titles, ISBN numbers and
descriptions) with Python. Is there a *simple* way to import an XML
file into a dictionary, list, or other usable data structure? I've
poked around with minidom, elementtree, and "untangle" but am not
really understanding how they a
> The simplest way using the standard library tools is (IMHO)
> elementtree. minidom is a complex beast by comparison,
> especially if you are not intimately familiar with
> your XML structure.
Thank you, this is helpful. Minidom is confusing, even the
documentation confirms this:
"The name of th
> Thank you, this is helpful. Minidom is confusing, even the
> documentation confirms this:
> "The name of the functions are perhaps misleading"
>
>> But I'd start with the etree tutorial (of which
>> there are many variations on the web):
Ok, so I read through these tutorials and am at least
> Yes, I personally think that (Mini)DOM should be locked away from beginners
> as far as possible.
Ok, I'm glad to hear that. I'll continue to work with ElementTree and
lxml and see where it takes me.
___
Tutor maillist - Tutor@python.org
To unsubscr
I'm working on a python script that runs on a Raspberry Pi. The script
detects when hardware buttons are pressed, and then runs functions based on
that input.
I want to be able to always listen for a button press, no matter what the
script is doing at the current moment. When a button press is d
> If you show us how you check whether the button is pressed, we may be able to
> show you how to run that asynchronously.
Apologies for the previous email; I think I sent it in HTML format.
Gmail changed their user interface again...
This is how I'm checking for a button press:
modes = (weather
On Sun, Nov 25, 2012 at 7:29 AM, Francois Dion wrote:
> This should really be done with interrupts, but unfortunately there is
> no support in the RPi.GPIO module for that, even if you have a patched
> kernel.
Thank you for all this great information. I ended up going with a
simple solution; I c
I'm attempting to use setup.py to build an RPM, but ran into this error:
[scarolan@cobbler:~/rpmbuild/BUILD/Python-2.7.3]$ python27 setup.py
bdist_rpm
File "setup.py", line 361
with open(tmpfile) as fp:
^
SyntaxError: invalid syntax
error: Bad exit status from /var/tmp/rpm-tmp.8
> Could it be that it is taking the system python executable which is
> probably 2.4?
>
> -Amit.
I've tried it with python24, python25 and python27 and all of them give the
same error.
___
Tutor maillist - Tutor@python.org
To unsubscribe or change sub
On Tue, Mar 26, 2013 at 10:18 AM, Sean Carolan wrote:
>
> Could it be that it is taking the system python executable which is
>> probably 2.4?
>>
>> -Amit.
>
>
> I've tried it with python24, python25 and python27 and all of them give
> the same error.
>
> What it looks like to me is that while you run (using python 2.7):
>
> > python27 setup.py bdist_rpm
>
> doing that generates a temporary bash script, which in turn runs:
>
> > python setup.py build
>
Yea, I checked this, and /usr/local/bin/python is just a symlink pointing
at /usr/local/bin/py
>
> http://hg.python.org/cpython/file/d321885ff8f3/Lib/distutils/command/bdist_rpm.py#l23
>
No dice.
[scarolan@titania:~/Python-2.7.3]$ alias | grep python
alias python='/usr/local/bin/python2.7'
[scarolan@titania:~/Python-2.7.3]$ /usr/local/bin/python2.7 setup.py
bdist_rpm
error: pyconfig.h: No
> If so, what was your secret?
>
>
I tried running this again with strace, and it looks like it's finding the
pyconfig.h file:
open("/usr/local/include/python2.7/pyconfig.h", O_RDONLY) = 4
read(4, "/* pyconfig.h. Generated from p"..., 4096) = 4096
stat("pyconfig.h", {st_mode=S_IFREG|0664, st_size
> Given that most folks on this list are only learning Python its pretty
> unlikely that they are building bespoke RPMs...
>
> You might find more experience of RPM building on the general Python
> mailing list/newsgroup.
Sorry 'bout that. I'll follow up with the bug report and possibly the
gene
> But, where did you get the idea that you could build Python RPMs using
> $python setup.py bdist_rpm ? I thought that was only limited to
> building RPMs for python packages (including extensions), but not the
> Python interpreter itself. Please correct me if i am wrong.
>
Ok, so it's only for mo
49 matches
Mail list logo