Skip to main content

Self-Use Python lib

Project description

Self-Use Python Lib

PyPI image image image

DirTree

image

Json To Object

Usage

more info in json_test_cases

import selfusepy
obj: One = selfusepy.parse_json(jsonStr, One())

Notice

Because Python is not a strongly-typed language, so you must
assign a value when you define a variable in class, 
otherwise the parser can not get the right type of each variable, 
just like examples below 

e.g. 1

Python Class

from selfusepy.jsonparse import BaseJsonObject
class One(BaseJsonObject):

  def __init__(self):
    self.x: str = ''  # have to be init
    self.two: One.Two = One.Two()  # have to be init

  class Two(BaseJsonObject):
    def __init__(self):
      self.y: str = ''
      self.three: One.Two.Three = One.Two.Three()

    class Three(BaseJsonObject):
      def __init__(self):
        self.z: str = ''

Json str

{
  "x": "x",
  "two": {
    "y": "y",
    "three": {
      "z": "z"
    }
  }
}

e.g. 2

Python Class

from selfusepy.jsonparse import BaseJsonObject
from typing import List
class One1(BaseJsonObject):

  def __init__(self):
    self.x: str = ''
    self.two: List[One1.Two] = [One1.Two()]

  class Two(BaseJsonObject):
    def __init__(self):
      self.y: str = ''

Json str

{
  "x": "x",
  "two": [
    {
      "y": "y1"
    },
    {
      "y": "y2"
    }
  ]
}

e.g. 4

from selfusepy.jsonparse import DeserializeConfig, BaseJsonObject
from selfusepy.utils import override_str
@override_str
@DeserializeConfig({'x--': 'x'})
class One2(BaseJsonObject):

  def __init__(self):
    self.x: str = ''
    self.two: One2.Two = One2.Two()

  @override_str
  @DeserializeConfig({'y--': 'y'})
  class Two(BaseJsonObject):
    def __init__(self):
      self.y: str = ''
      self.three: One2.Two.Three = One2.Two.Three()

    @override_str
    @DeserializeConfig({'z--': 'z'})
    class Three(BaseJsonObject):
      def __init__(self):
        self.z: str = ''

Json str

{
  "x--": "x",
  "two": {
    "y--": "y",
    "three": {
      "z--": "z"
    }
  }
}

e.g. 5

def handle(x):
  return datetime.fromisoformat(x)

@DeserializeConfig({"date": JsonField(func = handle)})
class Obj(BaseJsonObject):

  def __init__(self):
    self.date: datetime = datetime.now()

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

selfusepy-0.1.0.tar.gz (28.3 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: selfusepy-0.1.0.tar.gz
  • Upload date:
  • Size: 28.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.6

File hashes

Hashes for selfusepy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 82b3a57de8222748ee44ab75ab8564656207855f347c536e674cbbc70cfcf4f0
MD5 faceacb3a129375f571e7a1e0ac4f8f7
BLAKE2b-256 879610f5dbf752acfd42ab5df7988441b50597eb115c8f410697eb79d1eacbfe

See more details on using hashes here.

Supported by

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