Skip to content
This repository has been archived by the owner on May 31, 2020. It is now read-only.

Extend Builtin Format function #816

Merged
merged 26 commits into from
Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0de2bc2
work-in-progress: simple tests are passing
staujd02 Jun 6, 2019
212d27a
work-in-progress: working around float formatting
staujd02 Jun 7, 2019
51eb919
work-in-progress: working on padding with characters
staujd02 Jun 7, 2019
1515717
padding left and right work
staujd02 Jun 7, 2019
a57cd72
changed format to a redirect to native types format function
staujd02 Jun 12, 2019
d203fce
removed commented code
staujd02 Jun 12, 2019
6222146
updated to latest version
staujd02 Jun 12, 2019
b2cd878
wip: in a test deadlock
staujd02 Jun 12, 2019
a064f87
green tests: basic format types implemented
staujd02 Jun 12, 2019
d371e13
finished simple passthrough formats
staujd02 Jun 12, 2019
68f6a8c
replace interpolation with concats
Jun 12, 2019
e00cfca
Fixed formatting bugs
Jun 12, 2019
59e5579
fix: last linter checks
Jun 12, 2019
1cfab4f
fix: removed newline, thanks to vim
Jun 12, 2019
5eebdc6
wip: bool formatting
staujd02 Jun 13, 2019
1008ba5
implemented chunck of boolean __format__ function
staujd02 Jun 13, 2019
c58ee3b
brought in lastest changes
staujd02 Jun 13, 2019
0644b2f
clean-up
staujd02 Jun 13, 2019
4fc6e0d
format clean
staujd02 Jun 13, 2019
da8db0e
format clean: new at end of file
staujd02 Jun 13, 2019
eddbdfa
functional: bool formatting
Jun 17, 2019
c872e89
added documentation
Jun 17, 2019
41b4046
merged
Jun 17, 2019
8e2cff7
Merge branch 'master' of https://github.com/beeware/batavia into bett…
Jun 17, 2019
544ca78
fix: formatting
Jun 17, 2019
db81c40
fix: bytes_iter bad to string result
Jun 17, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions batavia/builtins/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ function format(args, kwargs) {
'format() takes at most 2 arguments (' + args.length + ' given)'
)
}
if(args[1] === ""){
return args[0];
}
if (!args[0].__format__) {
throw new exceptions.BataviaError.$pyclass(
'__format__ not implemented for this type.'
Expand Down
10 changes: 9 additions & 1 deletion batavia/types/Bool.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ var NotImplemented = require('../core').NotImplemented
/*************************************************************************
* Modify Javascript Boolean to behave like a Python bool
*************************************************************************/
const className = 'bool';

var Bool = Boolean

create_pyclass(Bool, 'bool', true)
create_pyclass(Bool, className, true)

Bool.prototype.__dir__ = function() {
var types = require('../types')
Expand Down Expand Up @@ -757,6 +758,13 @@ Bool.prototype.__trunc__ = function() {
return new types.Int(0)
}

Bool.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.NotImplementedError.$pyclass('bool.__format__ has not been implemented')
}

/**************************************************
* Module exports
**************************************************/
Expand Down
7 changes: 7 additions & 0 deletions batavia/types/Bytearray.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ Bytearray.prototype.__len__ = function() {
return this.val.__len__()
}

Bytearray.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.ValueError.$pyclass('ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className)
}

/**************************************************
* Module exports
**************************************************/
Expand Down
10 changes: 9 additions & 1 deletion batavia/types/BytearrayIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ var exceptions = require('../core').exceptions
/**************************************************
* Bytearray Iterator
**************************************************/
const className = 'bytearray_iterator';

function BytearrayIterator(data) {
PyObject.call(this)
this.index = 0
this.data = data
};

create_pyclass(BytearrayIterator, 'bytearray_iterator')
create_pyclass(BytearrayIterator, className)

BytearrayIterator.prototype.__iter__ = function() {
return this
Expand All @@ -33,6 +34,13 @@ BytearrayIterator.prototype.__str__ = function() {
return '<bytearray_iterator object at 0x99999999>'
}

BytearrayIterator.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.ValueError.$pyclass('ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className)
}

/**************************************************
* Module exports
**************************************************/
Expand Down
12 changes: 10 additions & 2 deletions batavia/types/Bytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ var NotImplemented = require('../core').NotImplemented
/*************************************************************************
* A Python bytes type
*************************************************************************/
const className = 'bytes';

function Bytes(val) {
// the value is an instance of Feross's Buffer class
PyObject.call(this)
this.val = val
}

create_pyclass(Bytes, 'bytes')
create_pyclass(Bytes, className)

Bytes.prototype.__dir__ = function() {
var types = require('../types')
Expand Down Expand Up @@ -310,7 +311,7 @@ Bytes.prototype.__getitem__ = function(other) {
var types = require('../types')

if (types.isinstance(other, types.Slice)) {
throw new exceptions.NotImplementedError.$pyclass('Bytes.__getitem__ with slice has not been implemented')
throw new exceptions.NotImplementedError.$pyclass('bytes.__getitem__ with slice has not been implemented')
}
if (!types.isinstance(other, types.Int)) {
throw new exceptions.TypeError.$pyclass('byte indices must be integers or slices, not ' + type_name(other))
Expand Down Expand Up @@ -363,6 +364,13 @@ Bytes.prototype.decode = function(encoding, errors) {
}
}

Bytes.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.ValueError.$pyclass('ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className)
}

/**************************************************
* Module exports
**************************************************/
Expand Down
7 changes: 7 additions & 0 deletions batavia/types/BytesIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ BytesIterator.prototype.__str__ = function() {
return '<bytes_iterator object at 0x99999999>'
}

BytesIterator.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.ValueError.$pyclass('ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className)
}

/**************************************************
* Module exports
**************************************************/
Expand Down
7 changes: 7 additions & 0 deletions batavia/types/CallableIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ CallableIterator.prototype.__str__ = function() {
return '<callable_iterator object at 0x99999999>'
}

CallableIterator.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.ValueError.$pyclass('ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className)
}

/**************************************************
* Module exports
**************************************************/
Expand Down
7 changes: 7 additions & 0 deletions batavia/types/Complex.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,13 @@ Complex.prototype.update = function(values) {
}
}

Complex.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.ValueError.$pyclass('ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className)
}

/**************************************************
* Module exports
**************************************************/
Expand Down
7 changes: 7 additions & 0 deletions batavia/types/Dict.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,13 @@ Dict.prototype.fromkeys = function(iterable, value) {
return d
}

Dict.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.ValueError.$pyclass('ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className)
}

/**************************************************
* Module exports
**************************************************/
Expand Down
4 changes: 4 additions & 0 deletions batavia/types/Enumerate.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ Enumerate.prototype.__str__ = function() {
return '<enumerate object at 0x99999999>'
}

Enumerate.prototype.__format__ = function (value, formatSpecifier) {
throw new exceptions.NotImplementedError.$pyclass('enumerate.__format__ has not been implemented')
}

/**************************************************
* Module exports
**************************************************/
Expand Down
7 changes: 7 additions & 0 deletions batavia/types/Filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ Filter.prototype.__str__ = function() {
return '<filter object at 0x99999999>'
}

Filter.prototype.__format__ = function(){
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.ValueError.$pyclass('ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className)
}

/**************************************************
* Module exports
**************************************************/
Expand Down
7 changes: 7 additions & 0 deletions batavia/types/Float.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,13 @@ Float.prototype.__trunc__ = function() {
return new types.Int(Math.trunc(this.valueOf()))
}

Float.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.ValueError.$pyclass('ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className)
}

/**************************************************
* Module exports
**************************************************/
Expand Down
4 changes: 4 additions & 0 deletions batavia/types/FrozenSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ FrozenSet.prototype._update = function(args) {
}
}

FrozenSet.prototype.__format__ = function(value, formatSpecifier) {
throw new exceptions.NotImplementedError.$pyclass('frozenset.__format__ has not been implemented')
}

/**************************************************
* Module exports
**************************************************/
Expand Down
6 changes: 5 additions & 1 deletion batavia/types/Function.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ Function.prototype.__get__ = function(instance) {
return new types.Method(instance, this)
}

Function.prototype.__format__ = function(value, formatSpecifier) {
throw new exceptions.NotImplementedError.$pyclass('function.__format__ has not been implemented')
}

/**************************************************
* Module exports
**************************************************/

module.exports = Function
module.exports = Function
7 changes: 7 additions & 0 deletions batavia/types/Int.js
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,13 @@ Int.prototype.__trunc__ = function() {
return this
}

Int.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.NotImplementedError.$pyclass('int.__format__ has not been implemented')
}

/**************************************************
* Module exports
**************************************************/
Expand Down
7 changes: 7 additions & 0 deletions batavia/types/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,13 @@ function validateIndexType(index) {
}
}

List.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.ValueError.$pyclass('ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className)
}

/**************************************************
* Module exports
**************************************************/
Expand Down
7 changes: 7 additions & 0 deletions batavia/types/ListIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ ListIterator.prototype.__str__ = function() {
return '<list_iterator object at 0x99999999>'
}

ListIterator.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.ValueError.$pyclass('ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className)
}

/**************************************************
* Module exports
**************************************************/
Expand Down
7 changes: 7 additions & 0 deletions batavia/types/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ Map.prototype.__str__ = function() {
return '<map object at 0x99999999>'
}

Map.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.ValueError.$pyclass('ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className)
}

/**************************************************
* Module exports
**************************************************/
Expand Down
7 changes: 7 additions & 0 deletions batavia/types/Method.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ function Method(instance, func) {
Method.prototype = Object.create(Function.prototype)
create_pyclass(Method, 'method', true)

Method.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.ValueError.$pyclass('ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className)
}

/**************************************************
* Module exports
**************************************************/
Expand Down
8 changes: 8 additions & 0 deletions batavia/types/Range.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ Range.prototype.__getitem__ = function(index) {
}
}

Range.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.ValueError.$pyclass('ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className)
'ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className
}

/**************************************************
* Module exports
**************************************************/
Expand Down
7 changes: 7 additions & 0 deletions batavia/types/RangeIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ RangeIterator.prototype.__str__ = function() {
return '<range_iterator object at 0x99999999>'
}

RangeIterator.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.ValueError.$pyclass('ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className)
}

/**************************************************
* Module exports
**************************************************/
Expand Down
7 changes: 7 additions & 0 deletions batavia/types/SequenceIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ SequenceIterator.prototype.__str__ = function() {
return '<iterator object at 0x99999999>'
}

SequenceIterator.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.ValueError.$pyclass('ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className)
}

/**************************************************
* Module exports
**************************************************/
Expand Down
7 changes: 7 additions & 0 deletions batavia/types/Set.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,13 @@ Set.prototype.update = function(args) {
}
}

Set.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.ValueError.$pyclass('ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className)
}

/**************************************************
* Module exports
**************************************************/
Expand Down
7 changes: 7 additions & 0 deletions batavia/types/SetIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ SetIterator.prototype.__str__ = function() {
return '<set_iterator object at 0x99999999>'
}

SetIterator.prototype.__format__ = function(value, formatSpecifier) {
if(formatSpecifier === ""){
return value.__str__()
}
throw new exceptions.ValueError.$pyclass('ValueError: Unknown format code' + formatSpecifier + 'for object of type ' + className)
}

/**************************************************
* Module exports
**************************************************/
Expand Down
4 changes: 4 additions & 0 deletions batavia/types/Str.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,10 @@ Str.prototype.capitalize = function() {
}
}

Str.prototype.__format__ = function(value, formatSpecifier) {
throw new exceptions.NotImplementedError.$pyclass('str.__format__ has not been implemented')
}

Str.prototype.format = function(args, kwargs) {
const types = require('../types')

Expand Down
Loading