Re: [Tutor] Responding Tweet: A Twitter Bot

2014-02-21 Thread Walter Prins
Hi,

On 21 February 2014 03:52, Zaki Akhmad  wrote:
> On Thu, Feb 20, 2014 at 7:39 PM, James Scholes  wrote:
>
>> Most decent Python libraries for accessing Twitter support the streaming
>> API.  This lets you keep a connection to the Twitter API alive and
>> process new data as it is received.  There is a simple (but out-of-date)
>> example on using streaming with the twitter package you linked to:
>> https://pypi.python.org/pypi/twitter/1.13.1
>
> My question is: how to execute this streaming API?
>
> My current approach is using cron to execute python script which has
> "check the streaming API" function:
>
> def check_mention:
> if (mention):
> tweet
>
> If I want to check every minute, then I should configure cron to
> execute this script every minute. Are there any other approach besides
> using cron?

With the caveat that I'm not familiar with the Twitter streaming API's
and that I literally only spend 3 minutes googling this, it seems to
me to be the case that the Twitter streaming API's is intended to be a
push style notification service.

This means you should not in principle ideally be polling the service
for updates yourself (e.g. using sleep/crong etc).  Instead, the docs
say that the streaming API can return an iterator that yields objects
as they're decoded from the stream.  Quote:

"The TwitterStream object is an interface to the Twitter Stream API
(stream.twitter.com). This can be used pretty much the same as the
Twitter class except the result of calling a method will be an
iterator that yields objects decoded from the stream. For example::"

It's highly preferable to not poll something if it will
generate/notify you of new objects, so you should be able to do
something like in their example.  Quote:

twitter_stream = TwitterStream(auth=UserPassAuth('joe', 'joespassword'))
iterator = twitter_stream.statuses.sample()

for tweet in iterator:
# ...do something with this tweet... (e.g. check if you want to
retweet or something)

So the for loop should just block by itself until a new tweet/message
comes in at which point it will spring to life and hand it to your
code to process.

I hope that helps, and apologies if I misunderstood something or have
missed something that makes my comment irrelevant to your problem.

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


[Tutor] from command prompt use interactive python and running script together

2014-02-21 Thread Gabriele Brambilla
Hi,

Is possible on python to running scripts from the command prompt (I'm using
python on windows) and in the end saving all the variables and continue the
analysis in the interactive mode? (the one that you activate typing python
in the command prompt?)
Or to use python in the interactive mode and in some moments to run
scripts, without quit() the interactive mode, and use for the script
variables the one you have defined in the interactive mode?


thanks

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


Re: [Tutor] from command prompt use interactive python and running script together

2014-02-21 Thread David Palao
2014-02-21 15:20 GMT+01:00 Gabriele Brambilla :
> Hi,
>
> Is possible on python to running scripts from the command prompt (I'm using
> python on windows) and in the end saving all the variables and continue the
> analysis in the interactive mode? (the one that you activate typing python
> in the command prompt?)

I guess you could define a function that does something like this by
using pickle or similar technology.

> Or to use python in the interactive mode and in some moments to run scripts,
> without quit() the interactive mode, and use for the script variables the
> one you have defined in the interactive mode?
>

You could import some objects from modules and use them in the
interactive session.

>
> thanks
>
> Gabriele
>

Best

> ___
> 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] from command prompt use interactive python and running script together

2014-02-21 Thread Chris “Kwpolska” Warrick
On Fri, Feb 21, 2014 at 3:20 PM, Gabriele Brambilla
 wrote:
> Hi,
>
> Is possible on python to running scripts from the command prompt (I'm using
> python on windows) and in the end saving all the variables and continue the
> analysis in the interactive mode? (the one that you activate typing python
> in the command prompt?)

Run this:

python -i file.py

> Or to use python in the interactive mode and in some moments to run scripts,
> without quit() the interactive mode, and use for the script variables the
> one you have defined in the interactive mode?
>
>
> thanks
>
> Gabriele
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>

-- 
Chris “Kwpolska” Warrick 
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] from command prompt use interactive python and running script together

2014-02-21 Thread eryksun
On Fri, Feb 21, 2014 at 9:20 AM, Gabriele Brambilla
 wrote:
>
> Is possible on python to running scripts from the command prompt (I'm using
> python on windows) and in the end saving all the variables and continue the
> analysis in the interactive mode? (the one that you activate typing python
> in the command prompt?)

The -i option will inspect interactively after running a command,
module, or script:

python -i [-c cmd | -m mod | file]

Or using the new launcher that comes with 3.3:

py [-2 | -3 | -X.Y | -X.Y-32] -i [-c cmd | -m mod | file]

The 3.3 installer associates the new launcher with .py files. This
introduces Unix-style shebang support, e.g.:

#!python2 -i

A shebang lets you run the script directly in the console, or from the
Windows GUI shell. In other words, instead of running "python -i
script.py" you'd simply run "script.py", or just double-click on the
file icon in Explorer. Normally the console window that opens when you
run a .py file will automatically close when the script exits. But
with -i in the above shebang, the console stays open, with the
interpreter running in interactive mode.

> Or to use python in the interactive mode and in some moments to run scripts,
> without quit() the interactive mode, and use for the script variables the
> one you have defined in the interactive mode?

You can use runpy.run_path, which was added in 2.7/3.2. It returns a
dict namespace, and if you want you can simply update globals() with
it.

test.py:

def main():
print('spam')

if __name__ == '__main__':
main()

Demo:

>>> import runpy
>>> ns = runpy.run_path('test.py', run_name='__main__')
spam
>>> globals().update(ns)
>>> main()
spam
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regarding Exceptions

2014-02-21 Thread wesley chun
On Mon, Feb 17, 2014 at 3:29 AM, Khalid Al-Ghamdi 
wrote:
Hi, in the following snippet, why is it I don't need to create an Exception
object and I can use the class directly in raise my custom exception?

here's a new one... i'm going to try to answer this question without
working code since everyone has pointed out it won't compile. the OP is
simply asking why Exception didn't need to be defined before using, as with
all Python variables... IOW, why doesn't it give a NameError exception here?

the reason is because Exception is a built-in, meaning that it's C code
that's been made available for you before your Python code executes. look:

>>> Exception


in reality, built-ins are part of a magical module called __builtins__
that's "automagically" imported for you so that you never have to do it
yourself. check this out:

>>> __builtins__.Exception


you can also find out what all the other built-ins are using dir():
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError',...]

hope this helps!
--wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A computer never does what you want... only what you tell it."
+wesley chun : wescpy at gmail : @wescpy
Python training & consulting : http://CyberwebConsulting.com
"Core Python" books : http://CorePython.com
Python blog: http://wescpy.blogspot.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regarding Exceptions

2014-02-21 Thread eryksun
On Fri, Feb 21, 2014 at 12:48 PM, wesley chun  wrote:
> in reality, built-ins are part of a magical module called __builtins__
> that's "automagically" imported for you so that you never have to do it
> yourself. check this out:
>
 __builtins__.Exception
> 
>
> you can also find out what all the other built-ins are using dir():
 dir(__builtins__)
> ['ArithmeticError', 'AssertionError', 'AttributeError',...]

In __main__, __builtins__ is the __builtin__ module (no "s"):

>>> __builtins__


I have no idea why. I guess someone thinks this is convenient? In
every other pure-Python (not built-in) module, it defaults to the dict
of the __builtin__ module:

>>> import __builtin__, os
>>> os.__builtins__ is vars(__builtin__)
True

That's the default when a module is executed. However, it's possible
to exec and eval code with a custom __builtins__:

>>> ns = {'__builtins__': {'msg': 'spam'}}
>>> exec 'print msg' in ns
spam
>>> eval('msg', ns)
'spam'

Just don't be deluded into thinking it's possible to sandbox Python like this.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] from command prompt use interactive python and running script together

2014-02-21 Thread Steven D'Aprano
On Fri, Feb 21, 2014 at 09:20:15AM -0500, Gabriele Brambilla wrote:
> Hi,
> 
> Is possible on python to running scripts from the command prompt (I'm using
> python on windows) and in the end saving all the variables and continue the
> analysis in the interactive mode? (the one that you activate typing python
> in the command prompt?)

The best way to do this is to use a Python shell that supports it. 
ipython is an advanced, powerful shell that supports saving and 
restoring the shell state when you leave, as well as many more features.

http://ipython.org/

The feature I think you want is this:

http://ipython.org/ipython-doc/stable/interactive/reference.html#session-logging-and-restoring

bpython is a more light-weight alternative. It doesn't have all the 
features of ipython, but it is closer to the standard interactive shell:

http://bpython-interpreter.org/

DreamPie is another alternate Python shell that might be worth 
investigating.

http://www.dreampie.org/



> Or to use python in the interactive mode and in some moments to run
> scripts, without quit() the interactive mode, and use for the script
> variables the one you have defined in the interactive mode?

Again, I expect that ipython will do something very like that.


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


Re: [Tutor] Regarding Exceptions

2014-02-21 Thread Steven D'Aprano
On Fri, Feb 21, 2014 at 01:27:55PM -0500, eryksun wrote:
> On Fri, Feb 21, 2014 at 12:48 PM, wesley chun  wrote:
> > in reality, built-ins are part of a magical module called __builtins__
> > that's "automagically" imported for you so that you never have to do it
> > yourself. check this out:
> >
>  __builtins__.Exception
> > 
> >
> > you can also find out what all the other built-ins are using dir():
>  dir(__builtins__)
> > ['ArithmeticError', 'AssertionError', 'AttributeError',...]
> 
> In __main__, __builtins__ is the __builtin__ module (no "s"):
> 
> >>> __builtins__
> 

Built-ins is confusing.

__builtins__ with an S is a special, implementation-specific hack for 
CPython only, it is *not* part of the language and you should not rely 
on it. Never use __builtins__, it is considered for internal use only.

In Python 2, the built-in objects live inside a module called 
__builtin__ (with no S). Like all modules, you have to import it before 
you can access the module. Unlike other modules, it's automatically 
used to look up names, even if you haven't imported it. So when you 
refer to a name like "foo", or "len", or "Exception", Python searches 
the local variables, the global variables, and the built-ins, before 
raising NameError if it is not found at all.

The difference between private __builtins__ and public __builtin__ is 
confusing and subtle and easy to get wrong, so in Python 3 the built-in 
module was renamed to "builtins".

# Don't do this, this is bad.
__builtins__.len

# Instead, do this in Python 2.
import __builtin__
__builtin__.len

# Or in Python 3.
import builtins
builtins.len



The history of __builtins__ with an S is quite old. It's used for 
performance reasons, and originally it was supposed to be used for 
sandboxing Python, but that turned out to not work. So although it still 
exists even in Python 3, it's a performance hack for the CPython 
interpreter, and does not exist in Jython, IronPython or other 
interpreters.


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


Re: [Tutor] Regarding Exceptions

2014-02-21 Thread eryksun
On Fri, Feb 21, 2014 at 8:50 PM, Steven D'Aprano  wrote:
> The history of __builtins__ with an S is quite old. It's used for
> performance reasons, and originally it was supposed to be used for
> sandboxing Python, but that turned out to not work. So although it still
> exists even in Python 3, it's a performance hack for the CPython
> interpreter, and does not exist in Jython, IronPython or other
> interpreters.

I can see how it's a small memory optimization to store __builtins__
in globals. Otherwise a function would need another slot for
func_builtins, just as a frame has f_globals and f_builtins. That's
assuming CPython retains the ability to use a custom builtins
namespace.

As you say, that isn't even possible in other Python implementations
such as Jython. As to this being a "performance hack", I can't see how
using __builtins__ improves performance, since the interpreter keeps a
reference to the dict of the __builtin__ module.

The attempt at sandboxing runs deeper than just customizing
__builtins__. Search the CPython 2.x source for PyEval_GetRestricted.
The latter returns true when a frame's f_builtins isn't the
interpreter's builtins. Restricted code can't directly create file
objects, unmarshal code objects (from .pyc), set old-style class
attributes, or read/write restricted members of built-in objects such
as a function's __doc__, among other restrictions.

But trying to lock Python down in an otherwise uncontrolled
environment is harder than playing Whac-A-Mole. Almost all of the
restricted API was removed from Python 3, except for a few legacy
flags.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor