Skip to main content

Python slurpers package for simple XML, JSON, config, etc. processing

Project description

Python slurpers for XML, JSON, configs (or .ini) and so on

Inspired by Groovy's XmlSlurper

Slurper is a object that encapsulates access to structured document file using its dynamic fields as file tags.

JSON example:

Json document:

{
	"name": "Joan",
	"surname": "Baez",
	"born": 1941,
	"albums": [
		{
			"name": "Folksingers 'Round Harvard Square",
			"year": 1959,
			"number-of-tracks": 18
		},
		{
			"name": "Joan Baez",
			"year": 1960,
			"number-of-tracks": 16
		},
		{
			"name": "Joan Baez, Vol. 2",
			"year": 1961,
			"number-of-tracks": 17
		},
		{
			"name": "Joan Baez/5",
			"year": 1964,
			"number-of-tracks": 14
		},
		{
			"name": "Farewell, Angelina",
			"year": 1965,
			"number-of-tracks": 14
		}
	]
}

code:

json = JsonSlurper.create(file_name = "testdata/baez.json", illegal_chars = Constants.STRIP_CAPITALIZE)
self.assertEqual("Joan", json.name)
self.assertEqual("Baez", json.surname)
self.assertEqual(1941, json.born)
self.assertEqual(5, len(json.albums))
self.assertEqual("Farewell, Angelina", json.albums[4].name)
self.assertEqual(14, json.albums[4].numberOfTracks)

Config example:

Sample config file:

[Database]
host: mysql.google.com
database: search_index

[Security]
auth provider: google

# and so on...

code:

config = ConfigSlurper.create(file_name = "testdata/google.config", illegal_chars = Constants.REPLACE_WITH_UNDERSCORES)
self.assertEqual("mysql.google.com", config.Database.host)
self.assertEqual("google", config.Security.auth_provider)

XML example:

Xml document (see in testdata/test1.xml):

<?xml version="1.0" encoding="utf-8"?>
<beatles>
  <man>
    <name>John</name>
    <name>Winston</name>
    <surname>Lennon</surname>
    <born>
        <place>Liverpool</place>
        <year>1940</year>
    </born>
  </man>
  <man>
    <name>James</name>
    <name>Paul</name>
    <surname>McCartney</surname>
    <born>
        <place>Liverpool</place>
        <year>1942</year>
    </born>
  </man>
  <man>
    <name>Ringo</name>
    <surname>Starr</surname>
    <born>
        <place>Liverpool</place>
        <year>1940</year>
    </born>
  </man>
  <man>
    <name>George</name>
    <surname>Harrison</surname>
    <born>
        <place>Liverpool</place>
        <year>1943</year>
    </born>
  </man>
</beatles>

Python code:

xml = XmlSlurper.create('testdata/test1.xml')
for man in xml.man:
    print('{} {} born at {} in {}'.format(' '.join(man.name), man.surname, man.born.year, man.born.place))

Output:

John Winston Lennon born at 1940 in Liverpool
James Paul McCartney born at 1942 in Liverpool
R i n g o Starr born at 1940 in Liverpool
G e o r g e Harrison born at 1943 in Liverpool

Starr and Harrison don't have second name, so man.name for them is a single string, so ' '.join() concatenates each char in it.

Lennon and McCartney are doublenamed, so man.name for them is list.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyslurpers-0.1.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

pyslurpers-0.1.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file pyslurpers-0.1.0.tar.gz.

File metadata

  • Download URL: pyslurpers-0.1.0.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.21.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.5

File hashes

Hashes for pyslurpers-0.1.0.tar.gz
Algorithm Hash digest
SHA256 331cf01f540b03c08dd5084b4f95401cf0db642ccf7e8131dcf5d36cb6e7a264
MD5 4c29ea64983290a6db5dacc2f6cabd34
BLAKE2b-256 98691b3433a4f266429b689d8122288a7ea7b033036f572ca02b4ad2d8d5412e

See more details on using hashes here.

File details

Details for the file pyslurpers-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyslurpers-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.21.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.5

File hashes

Hashes for pyslurpers-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f436d5603becf80c794d122e8051cedc5423cd3fff26c75485a4f51da1217012
MD5 b3824a90c6d01d805fa8c833e0207ca0
BLAKE2b-256 d49ac02a68b00b47d425ddba1e3d663ad1ddf75856b8b4a2373d805b97516a1c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page