Increment a VERSION ID by one and write to .mk file - python

I have code to read in the version number from a make file.
VERSION_ID=map(int,re.match("VERSION_ID\s*=\s*(\S+)",open("version.mk").read()).group(1).split("."))
This code takes VERSION_ID=0.0.2 and stores it as [0, 0, 2].
Is there any way I can increment this number by one and write the new version number into the version.mk file with the variable VERSION_ID.
Thanks
I have tried the same statement with write() instead of read() but I am getting an error saying that I can not write a list. I have also tried to write it as a string but am getting a bad file descriptor message.
s = str(VERSION_ID)
VERSION_ID=map(int,re.search("VERSION_ID\s*=\s*(\S+)",open("version.mk").write(s)).group(1).split("."))
I know this is rubbish, I just can't seem to find what to do here in the online docs.
I have also tried the pickle module to no avail. Maybe I'd be able to write a pickled list instead and then unpickle it. Or I was thinking I could just write over the whole line altogether.
I've tried to do anther approach, Ihave tried to get list to be entered as a string. I have tried this but I am not sure if it will work.
for x in VERSION_ID:
"VERSION_ID={0}.{1}.{2}.format(x)

perhpas something like this (you should also check for errors and that)
#! /usr/bin/python
import re
fn = "version.mk"
omk = open(fn).readlines()
nmk = open(fn, "w")
r = re.compile(r'(VERSION_ID\s*=\s*)(\S+)')
for l in omk:
m1 = r.match(l)
if m1:
VERSION_ID=map(int,m1.group(2).split("."))
VERSION_ID[2]+=1 # increment version
l = r.sub(r'\g<1>' + '.'.join(['%s' % (v) for v in VERSION_ID]), l)
nmk.write(l)
nmk.close()

Related

Is it possible to save the results of the "google" package to a txt file in python?

Link to program:
https://replit.com/#MichaelGordon5/DetailedSearch#main.py
I've coded a program that in essence, does an advanced google search, and Im planning on adding more to it later.
It uses the google search package
The issue I'm having with it is that when I print out the search results into the console:
#userquery = search term, amt= amount of results
for j in search(userquery, tld="com", num=amt, stop=amt, pause=0):
print(j)
I am unable to save the result to a text file. I have had some experience using f.write/open/close in the past but nothing I do seems to work. Any advice?
Note that I haven't actually tried your code, but the code below should work. I'm not sure if j needs to be cast as a str but it can't hurt.
sidenote: fix your indentation. It hurts my eyes. 4 spaces is what should always be used...
results = []
for j in search(userquery, tld="com", num=amt, stop=amt, pause=0):
print(j)
results.append(str(j))
with open("filename_goes_here.txt", "w") as outfile:
outfile.writelines(results)
Note that you could also keep the file open and write to it whenever you print out j...

using the input file object to read a variable from a file in python

So I'm thinking this is one of those problems where I can't see the forest for the tree. Here is the assignment:
Using the file object input, write code that read an integer from a file called
rawdata into a variable datum (make sure you assign an integer value to datum).
Open the file at the beginning of your code, and close it at the end.
okay so first thing: I thought the input function was for assigning data to an object such as a variable, not for reading data from an object. Wouldn't that be read.file_name ?
But I gave it shot:
infile = open('rawdata','r')
datum = int(input.infile())
infile.close()
Now first problem... MyProgrammingLab doesn't want to grade it. By that I mean I type in the code, click 'submit' and I get the "Checking" screen. And that's it. At the time of writing this, my latest attempt to submit as been 'checking' for 11 minutes. It's not giving me an error, it's just not... 'checking' I guess.
Now at the moment I can't use Python to try the program because it's looking for a while and I'm on a school computer that is write locked, so even if I have the code right (I doubt it), the program will fail to run because it can neither find the file rawdata nor create it.
So... what's the deal? Am I reading the instructions wrong or is it telling me to use input in some other way then I'm trying to use it? Or am I supposed to be using a different method?
You are so close. You're just using the file object slightly incorrectly. Once it's open, you can just .read() it, and get the value.
It would probably look something like this
infile = open('rawdata','r')
datum = int(infile.read())
infile.close()
I feel like your confusion is based purely on the wording of the question - the term "file object input" can certainly be confusing if you haven't worked with Python I/O before. In this case, the "file object" is infile and the "input" would be the rawdata file, I suppose.
Currently taking this class and figured this out. This is my contribution to all of us college peeps just trying to make it through, lol. MPL accepts this answer.
input = open('rawdata','r')
datum = int(input.readline())
input.close()

Specifying filename in os.system call from python

I am creating a simple file in python to reorganize some text data I grabbed from a website. I put the data in a .txt file and then want to use the "tail" command to get rid of the first 5 lines. I'm able to make this work for a simple filename shown below, but when I try to change the filename (to what I'd actually like it to be) I get an error. My code:
start = 2010
end = 2010
for i in range(start,end+1)
year = str(i)
...write data to a file called file...
teamname=open(file).readline() # want to use this in the new filename
teamfname=teamname.replace(" ","") #getting rid of spaces
file2 = "gotdata2_"+year+".txt"
os.system("tail -n +5 gotdata_"+year+".txt > "+file2)
The above code works as intended, creating file, then creating file2 that excludes the first 5 lines of file. However, when I change the name of file2 to be:
file2 = teamfname+"_"+year+".txt"
I get the error:
sh: line 1: _2010.txt: command not found
It's as if the end of my file2 statement is getting chopped off and the .txt part isn't being recognized. In this case, my code outputs a file but is missing the _2010.txt at the end. I've double checked that both year and teamfname are strings. I've also tried it with and without spaces in the teamfname string. I get the same error when I try to include a os.system mv statement that would rename the file to what I want it to be, so there must be something wrong with my understanding of how to specify the string here.
Does anyone have any ideas about what causes this? I haven't been able to find a solution, but I've found this problem difficult to search for.
Without knowing what your actual strings are, it's impossible to be sure what the problem is. However, it's almost certainly something to do with failing to properly quote and/or escape arguments for the command line.
My first guess would be that you have a newline in the middle of your filename, and the shell is truncating the command at the newline. But I wouldn't bet too heavily on that. If you actually printed out the repr of the pathname, I could tell you for sure. But why go through all this headache?
The solution to almost any problem with os.system is to not use os.system.
If you look at the docs, they even tell you this:
The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.
If you use subprocess instead of os.system, you can avoid the shell entirely. You can also pass arguments as a list instead of trying to figure out how to quote them and escape them properly. Which would completely avoid the exact problem you're having.
For example, if you do this:
file2 = "gotdata2_"+year+".txt"
with open(file2, 'wb') as f:
subprocess.check_call(['tail', '-n', '+5', "gotdata_"+year+".txt"], stdout=f)
Then, if you change that first line to this:
file2 = teamfname+"_"+year+".txt"
It will still work even if teamfname has a space or a quote or another special character in it.
That being said, I'm not sure why you want to use tail in the first place. You can skip the first 5 lines just as easily directly in Python.

python limit must be integer

I'm trying to run the following code but for some reason I get the following error: "TypeError: limit must be an integer".
Reading csv data file
import sys
import csv
maxInt = sys.maxsize
decrement = True
while decrement:
decrement = False
try:
**csv.field_size_limit(maxInt)**
except OverflowError:
maxInt = int(maxInt/10)
decrement = True
with open("Data.csv", 'rb') as textfile:
text = csv.reader(textfile, delimiter=" ", quotechar='|')
for line in text:
print ' '.join(line)
The error occurs in the starred line. I have only added the extra bit above the csv read statement as the file was too large to read normally. Alternatively, I could change the file to a text file from csv but I'm not sure whether this will corrupt the data further I can't actually see any of the data as the file is >2GB and hence costly to open.
Any ideas? I'm fairly new to Python but I'd really like to learn a lot more.
I'm not sure whether this qualifies as an answer or not, but here are a few things:
First, the csv reader automatically buffers per line of the CSV, so the file size shouldn't matter too much, 2KB or 2GB, whatever.
What might matter is the number of columns or amount of data inside the fields themselves. If this CSV contains War and Peace in each column, then yeah, you're going to have an issue reading it.
Some ways to potentially debug are to run print sys.maxsize, and to just open up a python interpreter, import sys, csv and then run csv.field_size_limit(sys.maxsize). If you are getting some terribly small number or an exception, you may have a bad install of Python. Otherwise, try to take a simpler version of your file. Maybe the first line, or the first several lines and just 1 column. See if you can reproduce the smallest possible case and remove the variability of your system and the file size.
On Windows 7 64bit with Python 2.6, maxInt = sys.maxsize returns 9223372036854775807L which consequently results in a TypeError: limit must be an integer when calling csv.field_size_limit(maxInt). Interestingly, using maxInt = int(sys.maxsize) does not change this. A crude workaround is to simlpy use csv.field_size_limit(2147483647) which of course cause issues on other platforms. In my case this was adquat to identify the broken value in the CSV, fix the export options in the other application and remove the need for csv.field_size_limit().
-- originally posted by user roskakori on this related question

NameError. Might be eval(), raw_input, or misunderstanding

Working with Python 2.6 and receiving an annoying error, and I'm still unsure why. My file of interest contains multiple lines of only a single value. I want to retrieve these values. This snippet of code
f = open(file, 'r')
for line in file:
value = eval(line)
results in the following error message:
File "<string>", line 1, in <module>
NameError: name 'c' is not defined
So I researched here on Stack Overflow.. with this question and another one.. but I'm having trouble drawing connections between their problems and mine. What I got from them is that my use of eval() may be confusing Python and I should use raw_input to let Python know that it doesn't have to evaluate line but rather the actual variables line represents. However, fixing my code to be:
for line in file:
value = eval(raw_input(line))
Which kicked out the following error (and may have overloaded terminal.. it simply froze up until I quit the program):
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
What am I doing wrong here? I've picked up the fact that eval() isn't the favorite to use around SO, am I misunderstanding it's function?
EDIT: My file is a list of values so,
2
3
2
3
1
0
etc
EDIT So it was a misunderstanding. Thank you to DSM for pointing out my file names being mismatched and to Levon for still helping out and showing int as a better alternative to eval.
If you just want to read those numbers from a file and convert them to integers this will do.
with open('data.txt') as f:
for line in f:
value = int(line)
Then you can use the value as needed.
Aside: The advantage of using with to open the file is that it is automatically closed for you when you are done, or an exception is encountered.
To respond to the title of your question, yes you are misunderstanding it. Let me explain:
eval executes the code you place in it. So x=eval('c') is the same as x=c.
raw_input is used to get user input. The parameter is the string to display to the user, not the input.
If you just want to read the values that are in a file, you could use
with open('file') as f:
for line in f:
var = int(line)
if you want to retrieve values from the files then this will do, using eval() for this doesn't make sense:
>>> with open('data.txt') as f:
values=[int(x) for x in f]
>>> values
[2, 3, 2, 3, 1, 0]

Categories