473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Using remote source code

Is there any possible way that I can place a .py file on the internet,
and use that source code in an .py file on my computer?

Mar 25 '07 #1
5 1700
<py*******@gmail.comwrote:
Is there any possible way that I can place a .py file on the internet,
and use that source code in an .py file on my computer?
You can write an import hook in any way you like; see
<http://www.python.org/dev/peps/pep-0302/.

Here's a trivial example (bereft of much error checking, etc). I've
uploaded to http://www.aleax.it/foo.py a toy module w/contents:

def foo(): return 'foo'

Here's a tiny program to import said module from my site:
import urllib2, sys, new

theurl = 'http://www.aleax.it/'

class Examp(object):
names = set([ 'foo', ])
def find_module(self, fullname, path=None):
if fullname not in self.names: return None
self.foo = urllib2.urlopen(theurl+fullname+'.py')
return self
def load_module(self, fullname):
module = sys.modules.setdefault(fullname,
new.module(fullname))
module.__file__ = fullname
module.__loader__ = self
exec self.foo.read() in module.__dict__
return module

def hooker(pathitem):
print 'hooker %r' % pathitem
if pathitem.startswith(theurl): return Examp()
raise ImportError

sys.path_hooks.append(hooker)
sys.path.append(theurl)

import foo
print foo.foo()

Alex
Mar 25 '07 #2
On Mar 25, 3:20 pm, a...@mac.com (Alex Martelli) wrote:
<pyappl...@gmail.comwrote:
Is there any possible way that I can place a .py file on the internet,
and use that source code in an .py file on my computer?

You can write an import hook in any way you like; see
<http://www.python.org/dev/peps/pep-0302/.

Here's a trivial example (bereft of much error checking, etc). I've
uploaded tohttp://www.aleax.it/foo.pya toy module w/contents:

def foo(): return 'foo'

Here's a tiny program to import said module from my site:

import urllib2, sys, new

theurl = 'http://www.aleax.it/'

class Examp(object):
names = set([ 'foo', ])
def find_module(self, fullname, path=None):
if fullname not in self.names: return None
self.foo = urllib2.urlopen(theurl+fullname+'.py')
return self
def load_module(self, fullname):
module = sys.modules.setdefault(fullname,
new.module(fullname))
module.__file__ = fullname
module.__loader__ = self
exec self.foo.read() in module.__dict__
return module

def hooker(pathitem):
print 'hooker %r' % pathitem
if pathitem.startswith(theurl): return Examp()
raise ImportError

sys.path_hooks.append(hooker)
sys.path.append(theurl)

import foo
print foo.foo()

Alex
Thanks for your help, now I can continue building my source code
generator. :)

Mar 25 '07 #3
Alex,

I see that you aren't using ihooks. Below is an example I found that
uses ihooks. I think it would be worth comparing and contrasting both
approaches (though I am not familar enough with this aspect of Python to
do so). IIRC, this code addresses some path related issues of other
import-from-file methods.

Note: This might not work from within ipython, but it works from within
Python.

"""
The ihooks module
This module provides a framework for import replacements. The idea is to
allow several alternate
import mechanisms to co-exist.

Example: Using the ihooks module
"""
import os
def writefile(f, data, perms=750): open(f, 'w').write(data) and
os.chmod(f, perms)

foobar = """
print "this is from the foobar module"

def x():
print "This is the x function."

"""

writefile('/tmp/foobar.py', foobar)
# File:ihooks-example-1.py
import ihooks, imp, os, sys
def import_from(filename):
"Import module from a named file"
if not os.path.exists(filename):
sys.stderr.write( "WARNING: Cannot import file." )
loader = ihooks.BasicModuleLoader()
path, file = os.path.split(filename)
name, ext = os.path.splitext(file)
m = loader.find_module_in_dir(name, path)
if not m:
raise ImportError, name
m = loader.load_module(name, m)
return m

foo = import_from("/tmp/foobar.py")

print foo.x
print foo.x()
print foo.x()


py*******@gmail.com wrote:
On Mar 25, 3:20 pm, a...@mac.com (Alex Martelli) wrote:
><pyappl...@gmail.comwrote:
>>Is there any possible way that I can place a .py file on the internet,
and use that source code in an .py file on my computer?
You can write an import hook in any way you like; see
<http://www.python.org/dev/peps/pep-0302/.

Here's a trivial example (bereft of much error checking, etc). I've
uploaded tohttp://www.aleax.it/foo.pya toy module w/contents:

def foo(): return 'foo'

Here's a tiny program to import said module from my site:

import urllib2, sys, new

theurl = 'http://www.aleax.it/'

class Examp(object):
names = set([ 'foo', ])
def find_module(self, fullname, path=None):
if fullname not in self.names: return None
self.foo = urllib2.urlopen(theurl+fullname+'.py')
return self
def load_module(self, fullname):
module = sys.modules.setdefault(fullname,
new.module(fullname))
module.__file__ = fullname
module.__loader__ = self
exec self.foo.read() in module.__dict__
return module

def hooker(pathitem):
print 'hooker %r' % pathitem
if pathitem.startswith(theurl): return Examp()
raise ImportError

sys.path_hooks.append(hooker)
sys.path.append(theurl)

import foo
print foo.foo()

Alex

Thanks for your help, now I can continue building my source code
generator. :)

--
Shane Geiger
IT Director
National Council on Economic Education
sg*****@ncee.net | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
Mar 25 '07 #4
py*******@gmail.com writes:
Is there any possible way that I can place a .py file on the internet,
and use that source code in an .py file on my computer?
Besides Alex suggestion, you can also check Pyro.

--
Jorge Godoy <jg****@gmail.com>
Mar 25 '07 #5
En Sun, 25 Mar 2007 04:06:21 -0300, Shane Geiger <sg*****@ncee.net>
escribió:
I see that you aren't using ihooks. Below is an example I found that
uses ihooks. I think it would be worth comparing and contrasting both
approaches (though I am not familar enough with this aspect of Python to
do so). IIRC, this code addresses some path related issues of other
import-from-file methods.
ihooks is rather old and undocumented except for a comment saying that it
"may become obsolete".
PEP 302 sets the recomended way now, AFAIK.

--
Gabriel Genellina

Mar 25 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
1
by: PRM | last post by:
Hi I have an ASp.net application using C#. I need to copy a file present on the web server machine to a remote machine on the network. Although this seems to be a trivial matter I am having...
0
by: PRM | last post by:
Hi I have an ASp.net application using C#. I need to copy a file present on the web server machine to a remote machine on the network. Although this seems to be a trivial matter I am having...
18
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't...
1
by: PRM | last post by:
Hi I have an ASp.net application using C#. I need to copy a file present on the web server machine to a remote machine on the network. Although this seems to be a trivial matter I am having...
0
by: D. Patrick | last post by:
I need to duplicate the functionality of a java applet, and how it connects to a remote server. But, I don't have the protocol information or the java source code which was written years ago. ...
1
by: kristine | last post by:
Hi, I've written an 'application' in DHTML and Javascript that runs on the client side. No ASP involved. For this 'application' I need to communicate with a database (MS Access 2002-2003), and...
0
by: sanjaygupta11 | last post by:
I am using httpwebrequest and httpwebresponse objects for sending data to remote appication by post and receiving the response from there. I am using this code in my windows application which will...
4
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, can someone please show me a working Example of how i can create a process on a remote system (does not need UI) with WMI using C#? Thanks in advance,... Regards
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.