Re: [Tutor] How to run a python script

2016-02-21 Thread Chad Perry
What version of Python are you using? Have you tried an



apt-get -y update

yum -y update





Sincerely,

Chad Perry
cperry...@hotmail.com
512.961.0291



From: Alan Gauld
Sent: Saturday, February 20, 2016 6:32 PM
To: tutor@python.org
Subject: Re: [Tutor] How to run a python script



On 20/02/16 16:25, Erol Gericke wrote:

> However I am unable to run any scripts.
>
> When I type (into the COMMAND PROMPT window) python test.py or
> python C:\python27\test.py  I get SyntaxError: invalid syntax

As Chris has hinted it sounds like you are running the script
at the wrong command prompt.

In general the Python prompt >>> is used to enter code
snippets directly.

The OS command prompt (C:\Windows> or similar) is used
to run Python script files.

To get an OS command prompt hit WindowsKey->Run and
type CMD and click OK. (You can also create a desktop
shortcut if you prefer.)

Also, for future reference it will help if you send
(cut n paste) the full error message not just a summary.
It contains a lot of useful data that helps pinpoint
the issue, once you learn to read it.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to run a python script

2016-02-21 Thread Alan Gauld
On 21/02/16 00:36, Chad Perry wrote:
> What version of Python are you using? Have you tried an
> apt-get -y update
> yum -y update


Since the OP appears to be running Windows I doubt
if that will help.

In fact, I doubt it would help with a syntax error
on Linux either.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to run a python script

2016-02-21 Thread Chris Warrick
Forwarding to mailing list, please use Reply All in the future.

On 21 February 2016 at 09:48, Erol Gericke  wrote:
> Hi Chris,
>
> Thanks for your prompt reply, the problem has been solved!
> I was using the 'python' terminal not the 'DOS' terminal.
>
>  I have created a new directory to hold the *.py scripts.
>
> As I type with only two fingers I want to keep everything as short as
> possible.
> Is there a way to avoid the full path when running a script.
>
> Thanks again, you have revived my interest in Python.
>
> Regards,
> Erol Gericke
>
>
>> Please paste the full error message, and your complete source code.
>> Also, make sure you are running the commands in the regular command
>> prompt window, and not in a Python-specific one.
>>
>> Also, your scripts should not live in C:\Python27, because they might
>> conflict with other things.
>>
>

You can open command prompt in the directory of your scripts [0] or
put them in a directory that doesn’t require a lot of typing (C:\py
for example).

PS. it’s not the “DOS” terminal, it’s the command prompt (cmd.exe).
DOS is not part of Windows NT/2000/XP and better, and rightfully so.

[0]: In Windows 8?/10, available from the File menu. Otherwise:
http://www.askvg.com/enable-open-command-window-here-option-in-context-menu-in-windows-vista/

-- 
Chris Warrick 
PGP: 5EAAEA16
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to run a python script

2016-02-21 Thread Erol Gericke

Thanks again
Erol

On 2016/02/21 11:10 AM, Chris Warrick wrote:

Forwarding to mailing list, please use Reply All in the future.

On 21 February 2016 at 09:48, Erol Gericke  wrote:

Hi Chris,

Thanks for your prompt reply, the problem has been solved!
I was using the 'python' terminal not the 'DOS' terminal.

  I have created a new directory to hold the *.py scripts.

As I type with only two fingers I want to keep everything as short as
possible.
Is there a way to avoid the full path when running a script.

Thanks again, you have revived my interest in Python.

Regards,
Erol Gericke



Please paste the full error message, and your complete source code.
Also, make sure you are running the commands in the regular command
prompt window, and not in a Python-specific one.

Also, your scripts should not live in C:\Python27, because they might
conflict with other things.


You can open command prompt in the directory of your scripts [0] or
put them in a directory that doesn’t require a lot of typing (C:\py
for example).

PS. it’s not the “DOS” terminal, it’s the command prompt (cmd.exe).
DOS is not part of Windows NT/2000/XP and better, and rightfully so.

[0]: In Windows 8?/10, available from the File menu. Otherwise:
http://www.askvg.com/enable-open-command-window-here-option-in-context-menu-in-windows-vista/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Finding line number by offset value.

2016-02-21 Thread Cody West
Hi,

I'm using yara-python for some file scanning and I'm trying to take the
offset in the 'strings' field and turn it into a line number.

http://yara.readthedocs.org/en/latest/yarapython.html

Below is what I'm working with.

(48L, '$execution', 'eval(base64_decode')

I'm trying to take 48L, which I believe is the character number, and get
the line number from that.

I know how to count lines until I match a string, but this is a little
harder.

I've searched all over the tubes and I can't seem to find anything useful.
Help a beginner out?

Thanks for your time,
Cody W
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to run a python script

2016-02-21 Thread Chad Perry
This is the shabang for Python

http://stackoverflow.com/questions/6908143/should-i-put-shebang-in-python-scripts-and-what-form-should-it-take

Sincerely,

Chad Perry
cperry...@hotmail.com
512.961.0291

> On Feb 21, 2016, at 3:11 AM, Chris Warrick  wrote:
> 
> Forwarding to mailing list, please use Reply All in the future.
> 
>> On 21 February 2016 at 09:48, Erol Gericke  wrote:
>> Hi Chris,
>> 
>> Thanks for your prompt reply, the problem has been solved!
>> I was using the 'python' terminal not the 'DOS' terminal.
>> 
>> I have created a new directory to hold the *.py scripts.
>> 
>> As I type with only two fingers I want to keep everything as short as
>> possible.
>> Is there a way to avoid the full path when running a script.
>> 
>> Thanks again, you have revived my interest in Python.
>> 
>> Regards,
>> Erol Gericke
>> 
>> 
>>> Please paste the full error message, and your complete source code.
>>> Also, make sure you are running the commands in the regular command
>>> prompt window, and not in a Python-specific one.
>>> 
>>> Also, your scripts should not live in C:\Python27, because they might
>>> conflict with other things.
> 
> You can open command prompt in the directory of your scripts [0] or
> put them in a directory that doesn’t require a lot of typing (C:\py
> for example).
> 
> PS. it’s not the “DOS” terminal, it’s the command prompt (cmd.exe).
> DOS is not part of Windows NT/2000/XP and better, and rightfully so.
> 
> [0]: In Windows 8?/10, available from the File menu. Otherwise:
> http://www.askvg.com/enable-open-command-window-here-option-in-context-menu-in-windows-vista/
> 
> -- 
> Chris Warrick 
> PGP: 5EAAEA16
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to run a python script

2016-02-21 Thread Chad Perry
Can you share the knowledge on what you did. I love Python it's easy...to use 
most of the time

Sincerely,

Chad Perry
cperry...@hotmail.com
512.961.0291

> On Feb 21, 2016, at 3:11 AM, Chris Warrick  wrote:
> 
> Forwarding to mailing list, please use Reply All in the future.
> 
>> On 21 February 2016 at 09:48, Erol Gericke  wrote:
>> Hi Chris,
>> 
>> Thanks for your prompt reply, the problem has been solved!
>> I was using the 'python' terminal not the 'DOS' terminal.
>> 
>> I have created a new directory to hold the *.py scripts.
>> 
>> As I type with only two fingers I want to keep everything as short as
>> possible.
>> Is there a way to avoid the full path when running a script.
>> 
>> Thanks again, you have revived my interest in Python.
>> 
>> Regards,
>> Erol Gericke
>> 
>> 
>>> Please paste the full error message, and your complete source code.
>>> Also, make sure you are running the commands in the regular command
>>> prompt window, and not in a Python-specific one.
>>> 
>>> Also, your scripts should not live in C:\Python27, because they might
>>> conflict with other things.
> 
> You can open command prompt in the directory of your scripts [0] or
> put them in a directory that doesn’t require a lot of typing (C:\py
> for example).
> 
> PS. it’s not the “DOS” terminal, it’s the command prompt (cmd.exe).
> DOS is not part of Windows NT/2000/XP and better, and rightfully so.
> 
> [0]: In Windows 8?/10, available from the File menu. Otherwise:
> http://www.askvg.com/enable-open-command-window-here-option-in-context-menu-in-windows-vista/
> 
> -- 
> Chris Warrick 
> PGP: 5EAAEA16
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] library terminology and importing

2016-02-21 Thread street . sweeper
Hello all,

I often use now() and strftime() from datetime, but it seems like I
can't import just those functions.  The os module allows me to import
like this:

from os.path import join,expanduser

but I get an error if I try

from datetime.datetime import now, strftime

But if I import all of os and datetime, I can use those functions by
writing the full 'path' 3 levels deep:

os.path.expanduser('~')
datetime.datetime.now()

Is there a way to import individual functions from datetime.datetime?


Also, is there proper terminology for each of the 3 sections of
os.path.expanduser('~') for example?  Such as

os - library (or module?)
path - ?
expanduser - function


Thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Finding line number by offset value.

2016-02-21 Thread Alan Gauld
On 21/02/16 19:32, Cody West wrote:
> I'm using yara-python for some file scanning and I'm trying to take the
> offset in the 'strings' field and turn it into a line number.

I know nothing about yara except that its some kind of
pattern matching engine. However...

> (48L, '$execution', 'eval(base64_decode')
> 
> I'm trying to take 48L, which I believe is the character number, and get
> the line number from that.

I'm not totally clear what you mean but, if it is that 48L
is the character count from the start of the file and you
want to know the line number then you need to count the
number of \n characters between the first and 48th
characters.

But thats depending on your line-end system of course,
there may be two characters on each EOL... It depends
on your OS/version and possibly the character encoding
too. And if it's a binary file, who knows, as there
won't really be any line endings. And, as I understand
it, yara is targeted at reading byte patterns from
binary files?

Are you sure you really need the line number?


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] library terminology and importing

2016-02-21 Thread Ben Finney
street.swee...@mailworks.org writes:

> I get an error if I try
>
> from datetime.datetime import now, strftime

‘datetime.datetime’ is not a module, so you can not import objects from
it.

> But if I import all of os and datetime, I can use those functions by
> writing the full 'path' 3 levels deep:
>
> os.path.expanduser('~')
> datetime.datetime.now()

Yes. That's a good way to do it, because it makes your code explicit and
clear to read.

> Is there a way to import individual functions from datetime.datetime?

Don't try to do that. Namespaces are a honking good idea in Python, you
should not seek to avoid them.

-- 
 \ “I may disagree with what you say, but I will defend to the |
  `\death your right to mis-attribute this quote to Voltaire.” |
_o__)   —Avram Grumer, rec.arts.sf.written, 2000-05-30 |
Ben Finney

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Finding line number by offset value.

2016-02-21 Thread Steven D'Aprano
On Mon, Feb 22, 2016 at 01:41:42AM +, Alan Gauld wrote:
> On 21/02/16 19:32, Cody West wrote:

> > I'm trying to take 48L, which I believe is the character number, and get
> > the line number from that.
> 
> I'm not totally clear what you mean but, if it is that 48L
> is the character count from the start of the file and you
> want to know the line number then you need to count the
> number of \n characters between the first and 48th
> characters.
> 
> But thats depending on your line-end system of course,
> there may be two characters on each EOL... 

Provided your version of Python is built with "universal newline 
support", and nearly every Python is, then if you open the file in text 
mode, all end-of-lines are automatically converted to \n on reading.

If the file is small enough to read all at once, you can do this:

offset = 48
text = the_file.read(offset)
print text.count('\n')


to print a line number starting from 0.

If the file is too big to read all at once, you can do this:

# untested
running_total = 0
line_num = -1
offset = 4800  # say
for line in the_file:
running_total += len(line)
line_num += 1
if running_total >= offset:
print line_num
break


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] library terminology and importing

2016-02-21 Thread Martin A. Brown

Good morning,

I'm inverting the order of your questions, because I think the order of the
answers may help.

>But if I import all of os and datetime, I can use those functions by
>writing the full 'path' 3 levels deep:
>
>os.path.expanduser('~')
>datetime.datetime.now()

[... hold onto your hat, we'll get to datetime.datetime ...]

>from os.path import join,expanduser
>
>Also, is there proper terminology for each of the 3 sections of
>os.path.expanduser('~') for example?  Such as

Yes, there most certainly is; it's good that you are asking.  See below.

>os - library (or module?)
>path - ?
>expanduser - function

Here's how you could figure out what they are called.  Use 'type' to figure
it out:

  >>> import os
  >>> type(os)
  

  >>> type(os.path)
  

  >>> type(os.path.expanduser)
  

Observe that the type of os.path.expanduser is function.

It is for this reason that you can importi (and use) the expanduser 
function by itself.  It can stand alone:

  >>> from os.path import expanduser
  >>> type(expanduser)
  

Side note, for diagnostics, 'type' can be handy, also, for things 
like:

  >>> type('word')
  
  >>> type(7)
  

>I often use now() and strftime() from datetime, but it seems like I can't
>import just those functions.  The os module allows me to import like this:

Ok, so back to datetime...

  >>> type(datetime)
  

This should not surpise you.  So, datetime is a module.  Good.

  >>> type(datetime.datetime)
  

Oh-ho!  What is this one?  It's called 'type'?  Well, it's a Python 
class.  You can see it in the source code, if you look for the class 
definition of 'datetime' in the module 'datetime'.  I find mine in 
/usr/lib64/python3.4/datetime.py around line 1290ff.  Look for this:

  class datetime(date):
  """datetime(year, month, day[, hour[, minute[, second[, 
microsecond[,tzinfo])

Why am I pointing you to this?  Well, in particular, you should see the
following a few lines later (lines 1394 ff in my copy):

@classmethod
def now(cls, tz=None):
"Construct a datetime from time.time() and optional time zone info."
t = _time.time()
return cls.fromtimestamp(t, tz)

If you wish, you can go look up the decorator @classmethod and what 
it does, but the main point I'm making here is that this is not a 
function!  It cannot be separated from the datetime class.  It is 
(in this case) an alternate constructor for a datetime object.  And, 
'type' will tell you so:

  >>> type(datetime.datetime.now)
  

So, even though the name is available to you and callable, when you 
import the module datetime, you can't separate the classmethod 
called 'now()' from the datetime.datetime class.

>but I get an error if I try
>
>from datetime.datetime import now, strftime

If you are mostly interested in shortening your import statement, I have seen
people use this sort of technique:

  >>> from datetime import datetime as dt
  >>> now = dt.now()
  >>> now.strftime('%F-%T')
  '2016-02-21-18:30:37'

Good luck and enjoy,

-Martin

-- 
Martin A. Brown
http://linux-ip.net/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor