Coder Social home page Coder Social logo

python-anywhere's Introduction

Overview

Anywhere provides a set of modules to work with resources that are identified by a URL. It comes with a set of handlers that can be extended.

It is licensed under a BSD 2-Clause license (see LICENSE.txt).

The Resource type

The Resource type abstracts access to a resource identified by a URL that contains:

  • the protocol
  • the path
>>> remote = Resource('ssh://storage1/path/to/file.json')

Every Resource implements the interface:

  • path: full path without the protocol
  • str returns the URL
  • name
  • size
  • atime
  • mtime
  • ctime
  • get()
  • put()
  • delete()
  • read()

Examples

These examples come from the filesystem interface in anywhere.resource.handler.filesystem.

Handling a file

Let's start by importing some modules to compare the resource interface with the Python standard library:

>>> import tempfile
>>> import os.path
>>> import shutil

Now we create a temporary file and wrap it in a FileResource:

>>> tmp = tempfile.NamedTemporaryFile()
>>> file = Resource(tmp.name)
>>> file # doctest: +ELLIPSIS
<FileResource file:///tmp/tmp...>
>>> str(file) # doctest: +ELLIPSIS
'file:///tmp/tmp...'
>>> file.path == tmp.name
True
>>> file.name == os.path.basename(tmp.name)
True
>>> file.size
0

The file is currently empty. We can append a line to it to add some content:

>>> file.append('First line.')
>>> file.read()
'First line.'

We can also add several lines at the same time:

>>> file.extend(['Second line.', 'Third line.'])
>>> file.read()
'Second line.\\nThird line.'

Handling a directory

We continue by creating a temporary directory:

>>> tmpdir = tempfile.mkdtemp()
>>> dir = Resource(tmpdir)

The file is not inside:

>>> file in dir
False

Let's copy it into the directory:

>>> dir.add(file)
>>> file in dir
True
>>> list(dir) == [file.name]
True
>>> file_alias = dir[file.name]
>>> file_alias.read() == file.read()
True

By default meth:FileResource.add overwrites the file if it already exists in the directory:

    >>> dir.add(file)

Setting the parameter overwrite to False allows to prevent from overwriting the file:

>>> dir.add(file, overwrite=False) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
IOError: '...' already exists in '/tmp/...'

>>> dir.remove(file)
>>> list(dir)
[]

Beware the a file alias may reference a file that was removed:

>>> file_alias.read() #doctest: +ELLIPSIS
Traceback (most recent call last):
...
IOError: [Errno 2] No such file or directory: '/tmp/...'

python-anywhere's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.