Another way, returning a list of integers
>>> import re
>>> def getints(string):
... return [int(x) for x in re.findall(r'\d+', string)]
...
>>> getints("foo23bar01-14qux2")
[23, 1, 14, 2]
Another way, returning a list of integers
>>> import re
>>> def getints(string):
... return [int(x) for x in re.findall(r'\d+', string)]
...
>>> getints("foo23bar01-14qux2")
[23, 1, 14, 2]
Use this python script
#!/usr/bin/env python
#-*-coding: utf8-*-
import argparse
import os
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='chmod files to mode 744')
parser.add_argument('files', nargs='+', metavar='FILES')
args = parser.parse_args()
for filename in args.files:
os.chmod(filename, 0744)
Replace the blocks
for item in data:
writer.writerow(data)
with
writer.writerow(data)
oh ok since i never copy anyone content, guess i'm fine?
It's not really the question. As a helper, I think it is useful to know that you are already getting help from someone else, not least because I can check if you already have a satisfactory answer. You already started many threads about different aspects of your program, and I think other helpers might want to check all the answers that you got.
I could not reproduce the bug. Here is my idle3 screen:
Oh, I see that you solved the issue. Great!
I tried your code, and apparently it works: the decimal points are aligned. Can you post your output ?
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> num1 = 127.899
>>> num2 = 3456.148
>>> num3 = 3.776
>>> num4 = 264.821
>>> num5 = 88.081
>>> num6 = 799.999
>>> print(format(num1, '7.2f'))
127.90
>>> print(format(num2, '7.2f'))
3456.15
>>> print(format(num3, '7.2f'))
3.78
>>> print(format(num4, '7.2f'))
264.82
>>> print(format(num5, '7.2f'))
88.08
>>> print(format(num6, '7.2f'))
800.00
>>>
Using readlines()
is a bad idea as it reads the whole file in memory. You could try something like the following code, which loads one line at a time
#!/usr/local/bin/python2.7
from __future__ import print_function
search_phrase = "Jul"
with open('input.txt','r') as f:
data = enumerate(f, 1)
try:
while True:
line_num, line = next(data)
if search_phrase in line:
print(line_num, line[2:26])
nums = set([line_num + 2, line_num + 5, line_num + 8])
while nums:
line_num, line = next(data)
if line_num in nums:
print(line_num, line)
nums.discard(line_num)
except StopIteration:
pass
The strange thing is that you open the file within a loop. It means that the same file is opened repeatedly by the program. Normally you open the output file once and loop to write a series of records.
Your code is very difficult to understand. I think you can do a lot with a single regular expression, like in this example
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 25 19:23:53 2014
python 2 or 3
@author: Gribouillis
"""
import re
pattern = re.compile(
r"^(?P<prefix>(?:[^_.]|_(?!p))+)"
r"_p(?P<type>(?:[^_.]|_(?!b))+)"
r"_b(?P<bed>(?P<num>[^h.]+)(?P<half>h?))\.csv",
flags = re.I
)
if __name__ == "__main__":
for filename in [
"test_PAQT_B2H.csv",
"test_PAQT_B4.csv",
"test_PINI_B1H.csv",
]:
match = pattern.match(filename)
print(match.groupdict())
""" my output --->
{'prefix': 'test', 'num': '2', 'type': 'AQT', 'bed': '2H', 'half': 'H'}
{'prefix': 'test', 'num': '4', 'type': 'AQT', 'bed': '4', 'half': ''}
{'prefix': 'test', 'num': '1', 'type': 'INI', 'bed': '1H', 'half': 'H'}
"""
I tried with the 22 data rows above. I had to replace line 36 with plt.show()
and comment line 11. It seems to work and I don't know what you mean by the program will hang. I'm on linux x86_64 with python 2.7.
Replace line 20 by the whole block of code.
Use
time_format = '%d/%m/%Y %H:%M'
try:
datetime_obj = datetime.strptime(time_string, time_format)
except ValueError:
print("STRING: ", repr(time_string), "FORMAT: ", repr(time_format))
raise
and post the output.
An alternative to what I wrote above is to create a per user site-packages directory (pep 370). In my kubuntu system it means typing
$ mkdir --parents ~/.local/lib/python2.7/site-packages
in a terminal. This directory is then automatically added to the python path when I run python. I can add my own scripts to the directory. For example if I have a file ~/Documents/daniweb/bargr.py
, I can add a symbolic link
$ ln -s $HOME/Documents/daniweb/bargr.py $HOME/.local/lib/python2.7/site-packages/bargr.py
then I can use import bargr
in python code from anywhere under my user name.
Another thing I can do with this directory is install modules from pypi in it, for example
$ pip install --user tabula
installs the pypi package tabula in my per-user site packages directory.
(I don't know what it does and there is no documentation, so
$ pip uninstall tabula
works too :)
Try
import eyeD3
To answer your second question, create for example a directory with
$ mkdir ~/pymods
in a terminal, then add the following lines to the file ~/.bashrc
, and restart the terminal
PYTHONPATH="$HOME/pymods:$PYTHONPATH"
export PYTHONPATH
Now every python file added to the directory ~/pymods
becomes importable
from anywhere (under your user name). For example create a file~/pymods/abracadabra.py
and use import abracadabra
from anywhere.
You can also store python packages in pymods (directories containing a file __init__.py
)
You can also add symbolic links to python files and packages.
There are other techniques for this, but it should be a starting point.
One part of the question is why would we need an OS written in python ?
My advice is: go linux !
As sepp2k said, create MyTime instances t1 = MyTime(9, 59, 59)
, etc.
Yes, only
name
in method FileName()
import *
as much as possible..
if __name__ == '__main__':
X = returnDataFrame()
X.FileName('F:/Python/Week05052013.csv')
WeeklyFile = DataFrame()
WeeklyFile = WeeklyFile.append(X.returnFile())
Here is a short explanation about variable visibility
varA = "foo" # a global variable
class Bar: # Bar is another global variable
varB = "baz" # a class variable
def method(self, arg): # self and arg are local variables
varC = "qux" # a local variable
self.name = "aname" # an instance variable
print( varA ) # no problem. global variables are visible
print( Bar.varB ) # varB is an attribute of class Bar
print( self.varB ) # varB can be accessed from instances as well
print( varC ) # No problem
print( varB ) # NameError. varB is not visible (nor global nor local)
Conclusion: import pandas in global namespace, not in class namespace.
Can you post a short code with mode = 'w'
, which doesn't rewrite the file ?
Why not
fh = logging.FileHandler(
pjoin('specific','directory','logfile.log'),
mode = 'w',)
?
Btw, do you know how to copy users from active directory to another computer?
No I don't know. It looks like a question about the windows OS rather than the python language. You could start a new thread in the windows forum.
Here is a code which woks for me
import os
import shutil
import string
import logging
pjoin = os.path.join
source = '/home/eric/Documents/daniweb/480781/test' # pjoin('C:', 'test')
dest = '/home/eric/Documents/daniweb/480781/testing'
# set up logging to file
logger = logging.getLogger(source)
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('logfile.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
header= string.join (("Started.", "Source: %s"% source,"Destination : %s" % dest), "\r\n")
formatter = logging.Formatter("%(asctime)s %(message)s")
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)
_debug = logger.debug
_info = logger.info
_info(header)
for root, dirs, files in os.walk(source):
desti = dest + root.replace(source, '')
#if we're in a directory that doesn't exist in the destination folder
#then create a new folder
if not os.path.isdir(desti):
os.mkdir(desti)
_info('Directory created at: %s' % desti)
#loop through all files
for f in files:
#compute current (old) & new file locations
oldLoc = pjoin(root, f)
newLoc = pjoin(desti, f)
if not os.path.isfile(newLoc):
try:
shutil.copy2(oldLoc, newLoc)
_info('File is successfully copied: %s' % newLoc)
except IOError:
_info('File is already exists: %s' % newLoc)
try:
shutil.copymode(oldLoc, newLoc)
_info('The file has been copied with access: %s' % newLoc)
except IOError:
_debug('The file could not copied with access: %s' % newLoc)
I think logging.info()
should be logger.info()
.
If you want the names only (and not the values of the variables), you can use the dir()
function. Names with double underscores on both sides are normally reserved for python's internal mechanics. Here you have the global names
__builtins__
: the default module __builtin__
(without s) which contains the builtin functions.__name__
: the python name of the current module, which is the string '__main__'
for the main script, but it would be 'xml.dom.minidom'
if this code was in the module xml.dom.minidom.__package__
: the name of the package containing the current module if any. It would be 'xml.dom'
for module xml.dom.minidom.__doc__
: the documentation string of the current module if any.sys
: module sys if you wrote import sys
somewhere.In imported modules, there is usually also __file__
, the system path to the file where the module's code was loaded if any.
par in this case is a variable but in Python specifically its called a name
Exactly. The namespaces are also called symbol tables. The functions locals()
and globals()
return the current symbol tables as dictionaries. Notice that locals()
is a read-only dict.
In what way is the use of Parameters similar/different to Variables? Same thing?
The correct python concept is the namespace. At any given moment in the execution of python code, a certain number of names are defined in the current namespace. There is no difference between parameters and variables in this respect. For example in the following code
foo = 10
def bar(qux):
baz = 23
the local namespace during the execution of function bar()
contains the 2 names qux
and baz
, whereas the global namespace contains the names foo
and bar
, and a few other predefined names such as __name__
.
Apparently, the whole code depends on 2 values, fileFull
and fileAbriv
. So you can write a function
def func(fileFull, fileAbriv):
etc
However, this code, with lines repeated with different variable name case is not good. There must be some other way to write your code. Read about the DRY principle.
In python, the normal way to walk a directory is to use os.walk()
. I would do exactly this.
I like spyder. A nice feature is that you can start working with a good old plain python file, while many IDE want you to define a project first. It is also genuine free software !
In ubuntu, install with
$ sudo apt-get install spyder
tell me why the dimension of Koch's curve is log(4)/log(3)?
Suppose your Koch's curve is contained in a disk of radius 1, then by construction, it can be covered by 4 disks of radius 1/3. By induction, the curve can be covered by 4^n
disks of radius r=(1/3)^n
. We then apply the definition of the Hausdorff dimension. Take a number d
and sum the radius at the power d
. It gives a sum of
s = (4^n)(3^(-n*d) = exp(n(log(4)-d*log(3)))
You see that if d
is slightly above log(4)/log(3)
, this sum tends to zero when n
tends to infinity. It proves that the Hausdorff dimension of the curve is smaller or equal to log(4)/log(3)
. Some other argument is necessary to prove equality, but I think it should give you an idea.
Here is an explanation, first with a list
, then with a bytes
. char[counter:counter+1]
is a slice, which has the same type as char
, while char[counter]
is an element.
>>> L = list(range(100, 110))
>>> L
[100, 101, 102, 103, 104, 105, 106, 107, 108, 109]
>>> L[4]
104
>>> L[4:5]
[104]
>>> counter = 7
>>> L[counter]
107
>>> L[counter:counter+1]
[107]
>>> x = b'abcdefghijk'
>>> x[4]
101
>>> x[4:5]
b'e'
>>> print(x)
b'abcdefghijk'
>>> print(x.decode('utf-8'))
abcdefghijk
There is also artist.decode()
.
I think you are mistaking str
for bytes
in python 3. In python 3, str
are unicode strings (sequences of abstract unicode code points) and bytes
are arrays of small integers. Use b'\x00'
when bytes are needed and '\x00'
when str are needed. The key to survival is to separate clearly the 2 things. After f.read()
, artist is a bytes
. You can add comments to indicate this.
Add
print(repr(word))
print(list)
immediately before line 19 and post the output here.
Also 'list' and 'dict' are so common words in python that nobody uses them for variables.
You're getting the error because char
is of type bytes
, and char[counter]
is an integer. Also you initiliazed newchar as a str
. At line 15, you can't add a str and an int. You could write
newchar = b'' # initialize as bytes
counter = 0
while counter < 30 and char[counter]:
newchar = newchar + char[counter:counter +1] # bytes + bytes
counter = counter + 1
However there is a much simpler way to remove trailing zero bytes:
newchar = char.rstrip(b'\x00')
Last, but not least, I tried with pygame/examples/data/house_lo.mp3
and the 125 last bytes are all space characters followed by exactly 1 null character.
Python only wraps the lower level system functions which open a file to create a file descriptor or a pointer to file, and the corresponding read or write functions. So, for example you could ask in the C forum if fopen()
with mode 'w'
physically overwrites existing contents on the drive, or if it may write in a different place.
It is very nice. I see 2 directions onwards:
This is not a short example. I don't see where you need the args in the second script. I suggest another solution: write a third module, say handleargs.py
# handleargs.py
parser = argparse.ArgParser(...)
...
args = parser.parse_args()
then in first.py and second.py:
from handleargs import args
# now use args
If you could give an example with two short scripts first.py
and second.py
, it may help.
Is that the only way or is what im wanting to do even posisble
What you want to do is not at all clear.
Your explanation is unclear. I don't think there is an issue. For example if the first file contains
args = parser.parse_args()
you can later call foo(args)
and it doesn't matter if function foo()
was defined in the first or the second script.
You could perhaps use the aspects module. Sometimes I use a simple tracing module which I wrote using aspects. The advantage of aspects is that methods are wrapped in a non intrusive way. See this answer to ihatehippies in a thread.
Use '--'
for long arguments (more than 1 letter).
The best thing to do is to add print(args)
to your script and test it in a console with commands such as
$ python filename.py -h
$ python filename.py -b /home/foobar
I would add a long name such as '--basepath'
to the '-b'
option and use BASEPATH
as metavar.
Also I don't think passing a password as a command line option is a good idea, because OSes keep history of the terminal commands.
Finally, Doug Hellmann is always a good reference when it comes to standard library modules.
You can perhaps use tokenize.generate_tokens()
which can parse a line or any piece of codes and give you token position (column).
I see 2 tools which could give you more detail:
sys.settrace()
to define your own trace function and count the number of calls in each line (there is some programming effort to get something here).f.f_lasti
on the calling frame to get the precise bytecode instruction where the call was done. The bytecode could help you compute the position in source code.Also, read the important Note here, and delete explicitely frames assigned to a variable.