Skip to main content

Helpers for generating code.

Project description

https://travis-ci.org/Appdynamics/python-langutil.png?branch=develop:target:https://travis-ci.org/Appdynamics/python-langutil

Language Utility

Currently, this utility only outputs equivalent PHP code.

PHP

import langutil.php

Converting ‘scalars’

A scalar in PHP is anything that is not an object (in PHP: !is_object(1) === true).

# Booleans
langutil.php.generate_scalar(True) == 'true'
langutil.php.generate_scalar(True, True) == 'TRUE'

# None turns into null
langutil.php.generate_scalar(None) == 'null'
langutil.php.generate_scalar(None, True) == 'null'

# Numbers are untouched
langutil.php.generate_scalar(1) == '1'
langutil.php.generate_scalar(2.5) == '2.5'

# Strings use single quotes unless it is necessary to use double quotes
# Double quotes are only necessary if the character has any control codes
langutil.php.generate_scalar('php code!') == ''php code!''
langutil.php.generate_scalar('this string has\nnew lines') == '"this string has\nnew lines"'

Converting lists, tuples, dictionaries, etc

These types all resolve to the same type in PHP: the ‘PHP array’. The generate_array() function handles recursive structures but may fail to detect special objects’ underlying structure unless they implement methods like __dict__().

All strings (including keys for dictionaries) will default to single quotes unless double quotes are necessary.

The output will use new lines and two-space indents.

langutil.php.generate_array([]) == 'array();'
langutil.php.generate_array(tuple([])) == 'array();'
langutil.php.generate_array(set(tuple([]))) == 'array();'
langutil.php.generate_array({}) == 'array();'
List/set/tuple conversion
langutil.php.generate_array([
    1,
    2,
])

Output:

array(
    1,
    2,
);
Dictionary conversion

For dictionaries, order of keys is not guaranteed. In PHP, hashes are ordered.

langutil.php.generate_array({
  'special': '\n',
  'special\x05': 'a string',
  'key': 1,
  'list': [1,2,3],
})

Output:

array(
  'key' => 1,
  'list' => array(
    1,
    2,
    3,
  ),
  "special\x05" => 'a string',
  'special' => "\n",
);

Serialisation

Use php.serialize(data_arg) for serialising data in PHP’s special serialisation format. The module will try to use the module phpserialize for this first but has a fallback version in pure Python for simple values (does not handle ‘classes’, references, or ‘objects’).

Note that the types list, tuple, set, and dict become PHP arrays (the serialisation format requires ‘keys’ to be created for lists so these become integers but should be an equivalent ‘integer-based array’ in PHP (the number keys do not become string keys).

To unserialize, use php.unserialize(str_arg) which requires the phpserialize module.

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

LangUtil-0.1.5.tar.gz (4.4 kB view details)

Uploaded Source

File details

Details for the file LangUtil-0.1.5.tar.gz.

File metadata

  • Download URL: LangUtil-0.1.5.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for LangUtil-0.1.5.tar.gz
Algorithm Hash digest
SHA256 d1506088062b0055cfa0c1501dfff0f256d27591b25899cc90f7dbd448e2d9d1
MD5 eb6e9b835976ba387e99df3fa6089e29
BLAKE2b-256 c2ef1a7e9476ea74235d9ff5842a4e43f5dc666cc8130e7cf323895b52c5cd5c

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