Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions src/Fable.Python.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
<Compile Include="stdlib/Builtins.fs" />
<Compile Include="stdlib/Json.fs" />
<Compile Include="stdlib/Html.fs" />
<Compile Include="stdlib/Logging.fs" />
<Compile Include="stdlib/Math.fs" />
<Compile Include="stdlib/Random.fs" />
<Compile Include="stdlib/Os.fs" />
<Compile Include="stdlib/Queue.fs" />
<Compile Include="stdlib/String.fs" />
Expand Down
4 changes: 4 additions & 0 deletions src/stdlib/Builtins.fs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ type IExports =
abstract int: obj -> int
/// Object to float
abstract float: obj -> float
/// Convert to bytes
abstract bytes: byte[] -> byte[]
/// Convert string to bytes with encoding
abstract bytes: string * encoding: string -> byte[]

/// Return the largest item in an iterable or the largest of two or more arguments.
abstract max: 'T * 'T -> 'T
Expand Down
148 changes: 148 additions & 0 deletions src/stdlib/Logging.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/// Type bindings for Python logging module: https://docs.python.org/3/library/logging.html
module Fable.Python.Logging

open Fable.Core

// fsharplint:disable MemberNames

/// Logging levels
[<RequireQualifiedAccess>]
module Level =
[<Literal>]
let CRITICAL = 50

[<Literal>]
let FATAL = 50

[<Literal>]
let ERROR = 40

[<Literal>]
let WARNING = 30

[<Literal>]
let WARN = 30

[<Literal>]
let INFO = 20

[<Literal>]
let DEBUG = 10

[<Literal>]
let NOTSET = 0

/// Formatter for log records
[<Import("Formatter", "logging")>]
type Formatter(fmt: string, ?datefmt: string) =
member _.format(record: obj) : string = nativeOnly

/// Handler base type
[<Import("Handler", "logging")>]
type Handler() =
/// Set the logging level
member _.setLevel(level: int) : unit = nativeOnly
/// Set the formatter for this handler
member _.setFormatter(formatter: Formatter) : unit = nativeOnly

/// StreamHandler - logs to a stream (stderr by default)
[<Import("StreamHandler", "logging")>]
type StreamHandler(?stream: obj) =
inherit Handler()

/// FileHandler - logs to a file
[<Import("FileHandler", "logging")>]
type FileHandler(filename: string, ?mode: string) =
inherit Handler()

/// Logger instance
[<Import("Logger", "logging")>]
type Logger(name: string, ?level: int) =
/// Log a message with severity DEBUG
member _.debug(msg: string) : unit = nativeOnly
/// Log a message with severity INFO
member _.info(msg: string) : unit = nativeOnly
/// Log a message with severity WARNING
member _.warning(msg: string) : unit = nativeOnly
/// Log a message with severity ERROR
member _.error(msg: string) : unit = nativeOnly
/// Log a message with severity CRITICAL
member _.critical(msg: string) : unit = nativeOnly
/// Log a message with severity ERROR and exception info
member _.``exception``(msg: string) : unit = nativeOnly
/// Log a message with the specified level
[<Emit("$0.log(int($1), $2)")>]
member _.log(level: int, msg: string) : unit = nativeOnly
/// Set the logging level
[<Emit("$0.setLevel(int($1))")>]
member _.setLevel(level: int) : unit = nativeOnly
/// Get the effective logging level
[<Emit("$0.getEffectiveLevel()")>]
member _.getEffectiveLevel() : int = nativeOnly
/// Check if the logger is enabled for the specified level
[<Emit("$0.isEnabledFor(int($1))")>]
member _.isEnabledFor(level: int) : bool = nativeOnly
/// Add the specified handler to this logger
[<Emit("$0.addHandler($1)")>]
member _.addHandler(handler: Handler) : unit = nativeOnly
/// Remove the specified handler from this logger
[<Emit("$0.removeHandler($1)")>]
member _.removeHandler(handler: Handler) : unit = nativeOnly
/// Check if this logger has any handlers configured
[<Emit("$0.hasHandlers()")>]
member _.hasHandlers() : bool = nativeOnly
/// The name of the logger
member _.name: string = nativeOnly

[<Erase>]
type IExports =
/// Log a message with severity DEBUG on the root logger
abstract debug: msg: string -> unit
/// Log a message with severity INFO on the root logger
abstract info: msg: string -> unit
/// Log a message with severity WARNING on the root logger
abstract warning: msg: string -> unit
/// Log a message with severity ERROR on the root logger
abstract error: msg: string -> unit
/// Log a message with severity CRITICAL on the root logger
abstract critical: msg: string -> unit
/// Log a message with severity ERROR and exception info on the root logger
abstract ``exception``: msg: string -> unit
/// Log a message with the specified level on the root logger
abstract log: level: int * msg: string -> unit

/// Return a logger with the specified name
[<Emit("$0.getLogger($1)")>]
abstract getLogger: name: string -> Logger
/// Return the root logger
[<Emit("$0.getLogger()")>]
abstract getLogger: unit -> Logger

/// Do basic configuration for the logging system
[<Emit("$0.basicConfig($1...)")>]
[<NamedParams(fromIndex = 0)>]
abstract basicConfig:
?filename: string *
?filemode: string *
?format: string *
?datefmt: string *
?style: string *
?level: int *
?stream: obj *
?force: bool ->
unit

/// Disable all logging calls of severity level and below
[<Emit("$0.disable($1)")>]
abstract disable: level: int -> unit

/// Logging level constants
abstract DEBUG: int
abstract INFO: int
abstract WARNING: int
abstract ERROR: int
abstract CRITICAL: int

/// Logging facility for Python
[<ImportAll("logging")>]
let logging: IExports = nativeOnly
144 changes: 144 additions & 0 deletions src/stdlib/Random.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/// Type bindings for Python random module: https://docs.python.org/3/library/random.html
module Fable.Python.Random

open System.Collections.Generic
open Fable.Core

// fsharplint:disable MemberNames

[<Erase>]
type IExports =
/// Initialize the random number generator
/// See https://docs.python.org/3/library/random.html#random.seed
[<Emit("$0.seed(int($1))")>]
abstract seed: a: int -> unit
/// Initialize the random number generator
/// See https://docs.python.org/3/library/random.html#random.seed
abstract seed: a: nativeint -> unit
/// Initialize the random number generator
/// See https://docs.python.org/3/library/random.html#random.seed
abstract seed: a: float -> unit
/// Initialize the random number generator
/// See https://docs.python.org/3/library/random.html#random.seed
abstract seed: a: string -> unit
/// Initialize the random number generator with current time
/// See https://docs.python.org/3/library/random.html#random.seed
abstract seed: unit -> unit

/// Return a random floating point number in the range [0.0, 1.0)
/// See https://docs.python.org/3/library/random.html#random.random
abstract random: unit -> float

/// Return a random floating point number N such that a <= N <= b
/// See https://docs.python.org/3/library/random.html#random.uniform
abstract uniform: a: float * b: float -> float

/// Return a random floating point number N such that low <= N <= high with triangular distribution
/// See https://docs.python.org/3/library/random.html#random.triangular
abstract triangular: low: float * high: float * mode: float -> float
/// Return a random floating point number N such that 0 <= N <= 1 with triangular distribution
/// See https://docs.python.org/3/library/random.html#random.triangular
abstract triangular: unit -> float

/// Return a random integer N such that a <= N <= b
/// See https://docs.python.org/3/library/random.html#random.randint
abstract randint: a: int * b: int -> int

/// Return a randomly selected element from range(start, stop, step)
/// See https://docs.python.org/3/library/random.html#random.randrange
abstract randrange: stop: int -> int
/// Return a randomly selected element from range(start, stop, step)
/// See https://docs.python.org/3/library/random.html#random.randrange
abstract randrange: start: int * stop: int -> int
/// Return a randomly selected element from range(start, stop, step)
/// See https://docs.python.org/3/library/random.html#random.randrange
abstract randrange: start: int * stop: int * step: int -> int

/// Return a random element from the non-empty sequence
/// See https://docs.python.org/3/library/random.html#random.choice
[<Emit("$0.choice(list($1))")>]
abstract choice: seq: 'T[] -> 'T
/// Return a random element from the non-empty sequence
/// See https://docs.python.org/3/library/random.html#random.choice
[<Emit("$0.choice(list($1))")>]
abstract choice: seq: 'T list -> 'T
/// Return a random element from the non-empty sequence
/// See https://docs.python.org/3/library/random.html#random.choice
abstract choice: seq: ResizeArray<'T> -> 'T

/// Return a k length list of unique elements chosen from the population sequence
/// See https://docs.python.org/3/library/random.html#random.sample
[<Emit("$0.sample(list($1), int($2))")>]
abstract sample: population: 'T[] * k: int -> ResizeArray<'T>
/// Return a k length list of unique elements chosen from the population sequence
/// See https://docs.python.org/3/library/random.html#random.sample
[<Emit("$0.sample(list($1), int($2))")>]
abstract sample: population: 'T list * k: int -> ResizeArray<'T>
/// Return a k length list of unique elements chosen from the population sequence
/// See https://docs.python.org/3/library/random.html#random.sample
[<Emit("$0.sample($1, int($2))")>]
abstract sample: population: ResizeArray<'T> * k: int -> ResizeArray<'T>

/// Return a k sized list of elements chosen from the population with replacement
/// See https://docs.python.org/3/library/random.html#random.choices
[<Emit("$0.choices(list($1), k=int($2))")>]
abstract choices: population: 'T[] * k: int -> ResizeArray<'T>
/// Return a k sized list of elements chosen from the population with replacement
/// See https://docs.python.org/3/library/random.html#random.choices
[<Emit("$0.choices(list($1), k=int($2))")>]
abstract choices: population: 'T list * k: int -> ResizeArray<'T>
/// Return a k sized list of elements chosen from the population with replacement
/// See https://docs.python.org/3/library/random.html#random.choices
[<Emit("$0.choices($1, k=int($2))")>]
abstract choices: population: ResizeArray<'T> * k: int -> ResizeArray<'T>

/// Shuffle the sequence x in place
/// See https://docs.python.org/3/library/random.html#random.shuffle
abstract shuffle: x: 'T[] -> unit
/// Shuffle the sequence x in place
/// See https://docs.python.org/3/library/random.html#random.shuffle
abstract shuffle: x: ResizeArray<'T> -> unit

/// Return a random integer with k random bits
/// See https://docs.python.org/3/library/random.html#random.getrandbits
abstract getrandbits: k: int -> int

/// Beta distribution
/// See https://docs.python.org/3/library/random.html#random.betavariate
abstract betavariate: alpha: float * beta: float -> float

/// Exponential distribution
/// See https://docs.python.org/3/library/random.html#random.expovariate
abstract expovariate: lambd: float -> float

/// Gamma distribution
/// See https://docs.python.org/3/library/random.html#random.gammavariate
abstract gammavariate: alpha: float * beta: float -> float

/// Gaussian distribution (same as normalvariate but faster)
/// See https://docs.python.org/3/library/random.html#random.gauss
abstract gauss: mu: float * sigma: float -> float

/// Log normal distribution
/// See https://docs.python.org/3/library/random.html#random.lognormvariate
abstract lognormvariate: mu: float * sigma: float -> float

/// Normal distribution
/// See https://docs.python.org/3/library/random.html#random.normalvariate
abstract normalvariate: mu: float * sigma: float -> float

/// Von Mises distribution
/// See https://docs.python.org/3/library/random.html#random.vonmisesvariate
abstract vonmisesvariate: mu: float * kappa: float -> float

/// Pareto distribution
/// See https://docs.python.org/3/library/random.html#random.paretovariate
abstract paretovariate: alpha: float -> float

/// Weibull distribution
/// See https://docs.python.org/3/library/random.html#random.weibullvariate
abstract weibullvariate: alpha: float * beta: float -> float

/// Random variable generators
[<ImportAll("random")>]
let random: IExports = nativeOnly
63 changes: 62 additions & 1 deletion src/stdlib/String.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Type bindings for Python string operations: https://docs.python.org/3/library/stdtypes.html#string-methods
/// Type bindings for Python string module: https://docs.python.org/3/library/string.html
module Fable.Python.String

open System
Expand All @@ -10,3 +10,64 @@ type System.String with

[<Emit("$0.format($1...)")>]
member _.format([<ParamArray>] args: Object[]) = nativeOnly

/// Template class for $-based string substitution
[<Import("Template", "string")>]
type Template(template: string) =
/// The template string passed to the constructor
member _.template: string = nativeOnly

/// Perform substitution, returning a new string
/// Raises KeyError if placeholders are missing from mapping
[<Emit("$0.substitute($1)")>]
member _.substitute(mapping: obj) : string = nativeOnly

/// Perform substitution using keyword arguments
[<Emit("$0.substitute(**$1)")>]
member _.substituteKw(kwargs: obj) : string = nativeOnly

/// Like substitute(), but returns original placeholder if missing
[<Emit("$0.safe_substitute($1)")>]
member _.safe_substitute(mapping: obj) : string = nativeOnly

/// Like substitute(), but returns original placeholder if missing (keyword args)
[<Emit("$0.safe_substitute(**$1)")>]
member _.safe_substituteKw(kwargs: obj) : string = nativeOnly

/// Returns False if the template has invalid placeholders
[<Emit("$0.is_valid()")>]
member _.is_valid() : bool = nativeOnly

/// Returns a list of valid identifiers in the template
[<Emit("$0.get_identifiers()")>]
member _.get_identifiers() : ResizeArray<string> = nativeOnly

[<Erase>]
type IExports =
/// The concatenation of ascii_lowercase and ascii_uppercase
abstract ascii_letters: string
/// The lowercase letters 'abcdefghijklmnopqrstuvwxyz'
abstract ascii_lowercase: string
/// The uppercase letters 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
abstract ascii_uppercase: string
/// The string '0123456789'
abstract digits: string
/// The string '0123456789abcdefABCDEF'
abstract hexdigits: string
/// The string '01234567'
abstract octdigits: string
/// String of ASCII characters considered punctuation
abstract punctuation: string
/// String of ASCII characters considered printable
abstract printable: string
/// String containing all ASCII whitespace characters
abstract whitespace: string

/// Split the argument into words, capitalize each word, and join them
abstract capwords: s: string -> string
/// Split the argument into words using sep, capitalize each word, and join them
abstract capwords: s: string * sep: string -> string

/// Python string module
[<ImportAll("string")>]
let pyString: IExports = nativeOnly
Loading