Blog

  • scala-optics-monocle-quicklens-workshop

    Build Status License: GPL v3

    scala-optics-monocle-quicklens-workshop

    preface

    • goals of this workshop
      • introduction to theoretical basis of optics
      • practice monocle and quicklens libraries
    • workshop plan
      • provide implementation of lib.Lens and then use it in VanillaOpticsSpec
        • hint: use lib.Getter and lib.Setter
      • implement tests in OpticsSpec according to its names

    introduction

    • problem: updating nested immutable objects
      • example
        sealed trait PaymentMethod
        object {
            case class PayPal(emai: String) extends PaymentMethod
            case class DebitCard(
                cardNumber: String,
                expirationDate: YearMonth,
                securityCode: Int
            ) extends PaymentMethod
        }
        
        case class User(name: String, address: Address, paymentMethod: PaymentMethod)
        case class Address(streetNumber: Int, postCode: String)
        
        val michal = User("michal", Address(12, "E16 4SR"), PaymentMethod.PayPal("a@gmail.com"))
        
      • updates of mutable objects (assume that above we have fields defined as vars)
        michal.address.streetNumber = 16
        
      • updated of immutable objects
        • using copy
          michal.copy(address = michal.address.copy(streetNumber = 16)
          
        • but how we will update enum?
          def updateExpiry(user: User, newExpiry: YearMonth): User =
              user.copy(paymentMethod =
                  user.paymentMethod match {
                      case card: DebitCard => card.copy(expirationDate = newExpiry)
                      case paypal: PayPal => paypal
                  }
          
        • or suppose we have paymentMethods: Map[String, PaymentMethod] instead of single paymentMethod: PaymentMethod
          val michal = User(
              "michal",
              Address(12, "E16 4SR"),
              Map(
                  "Personal" -> PayPal("mtu@gmail.com"),
                  "Business" -> DebitCard("", YearMonth.of(2022, 7), 995)
              )
          )
          
          def updateExpiry(user: User, paymentName: String, newExpiry: YearMonth): User =
              user.copy(paymentMethod =
                  user.paymentMethods.get(paymentName) match {
                      case None | Some(_: PayPal) => user.paymentMethods
                      case Some(card: DebitCard) =>
                          val updatedCard = card.copy(expirationDate = newExpiry)
                          user.paymentMethods.updated(paymentName, updatedCard)
                  }
          
    • it would be nice if we could have something like OOP’s setters for immutable data structures
      • .copy is like that, but it doesn’t compose
      • lenses can be considered the functional representations of getters and setters
        case class Lens[A, B](get: A => B, set: (B, A) => A)
        
        and usage
        val articleTitleLens = Lens[Article, String](get = _.title, set = (t, a) => a.copy(title = t))
        
      • note that this boilerplate could be generated by macros

    use cases

    • working with deeply nested JSON
      • example
        import io.circe._, io.circe.parser._
        
        val json: Json = parse("""
        {
          "order": {
            "customer": {
              "name": "Custy McCustomer",
              "contactDetails": {
                "address": "1 Fake Street, London, England",
                "phone": "0123-456-789"
              }
            },
            "items": [{
              "id": 123,
              "description": "banana",
              "quantity": 1
            }, {
              "id": 456,
              "description": "apple",
              "quantity": 2
            }],
            "total": 123.45
          }
        }
        """).getOrElse(Json.Null)
        
      • migrate one JSON format into the other without having to decode into the ADT
        import io.circe.optics.JsonPath._
        
        val _phoneNum = root.order.customer.contactDetails.phone.string
        // _phoneNum: monocle.package.Optional[Json, String] = monocle.POptional$$anon$1@367b3ec4
        
        val phoneNum: Option[String] = _phoneNum.getOption(json)
        
      • receive events/requests in the older format, check version, run all migrations to the current one
        • keep case classes and case objects only for the current schema
      • modifying json
        val doubleQuantities: Json => Json = root.order.items.each.quantity.int.modify(_ * 2)
        
    • working with property based testing
      • introduction to property based testing: https://github.com/mtumilowicz/scala-zio2-test-aspects-property-based-testing-workshop
      • it is often easier to generate general complex object and then in test just change some fields according to needs
      • example
        val genAccount: Gen[Any, Account] = DeriveGen[Account]
        
        test("create locked account test") {
          check(genAccount) { account =>
            val lockedAccount = account.modify(_.details.alerting.locked).setTo(false)
            assertZIO(AccountService.createAccount(lockedAccount))(equalTo(AccountCreated(account.id)))
          }
        }
        

    lens

    A lens is a first-class reference to a subpart of some data type

    School of Haskell

    any data accessor for a component of a data structure is ‘function-like’, in the sense that reading ‘consumes’ the component from the data structure and writing ‘produces’ an updated component to put back into the data structure. The type structure of such function-like things — henceforth transformers — is technically known as a profunctor.

    Profunctor Optics Modular Data Accessors

    • problem: how to implement Lens for a sum type?
      • with current implementation: impossible
        • there is no way to define getter
      • solution: partial lens (prism/optional in monocle)
        case class PartialLens[A, B](get: A => Option[B], set: (B, A) => Option[A])
        
    • polymorphic lenses
      • lens construct focuses on a single field
      • what if we have multiple fields?
        • example: setting all the values in certain fields of a json
        • with current implementation: impossible
        • solution: val Laarhoven representation
          • we want to unify all functions
            get: S => A
            set: A => S => S
            modify: (A => A) => (S => S)
            modifyMaybe: (A => Option[A]) => (S => Option[S])
            modifyList: (A => List[A]) => (S => List[S])
            
          • generic representation the can support all features in the same type
            [F: Functor[_], S, T, A, B] // F ~ for example: Option, Collection
            modifyF: (f: A => F[B]): S => F[T]
            
      • when Optic is polymorphic additional two types come into play for “reverse” operation
        • example
          trait PLens[S, T, A, B] {
          	def get(s: S): A
          	def set(b: B)(s: S): T
          }
          
        • B for an argument and T for a result of that operation
      • Traversal is more generic than Optional
        • it composes with everything that Optional composes
        • it makes sense for all optics to have 4 type parameters (for S => T and A => B mappings)
          • this way we can always compose them
    • laws
      • identity: if you get and then set back with the same value, the object remains identical
        lens.set(s, lens.get(s)) == s
        
      • retention: if you set with a value and then perform a get, you get the same value back
        lens.get(lens.set(s, a)) == a
        
      • double set — if you set twice in succession and then perform a get, you get back the last set value
        lens.get(lens.set(lens.set(s, a), b)) == b
        
      • example of breaking a law
        michal.focus(_.paymentMethods
            .*
            .as[DebitCard]
            .filter(_.expirationDate.isAfter(YearMonth.of(2020, 5))) // breaks the contract
            .expirationDate
            ).get
        
        val optic = Focus[User](_.paymentMethods
            .*
            .as[DebitCard]
            .filter(_.expirationDate.isAfter(YearMonth.of(2020, 5))) // breaks the contract
            .expirationDate
        
        val updated1 = optic.modify(michal, _.minusYears(2))
        val updated2 = optic.modify(update1, _.plusMonths(3)) // not works
        val updated3 = optic.modify(michal, _.minusYears(2).plusMonths(3)) // not the same result as updated1 + updated2
        

    optics

    • based on Monocle
      • ISO[S, A]
        • converts elements of type S into elements of type A without loss
        • isomorphism between two types (bijection)
        • example
          • case class <-> tupleN
            case class Person(name: String, age: Int)
            
            • is equivalent to a tuple (String, Int)
            • tuple (String, Int) is equivalent to Person
          • collections: List <-> Vector
        • convenient to lift methods from one type to another
          • example: a String can be seen as a List[Char]
        • you can think of Iso as something that is simultaneously Lens and Prism
          • Lens: navigating from S to A is always successful
          • Prism: navigating from A to S does not need any additional information besides of A value
          • in other words: transformation from S to A is lossless
        • when is useful?
          • anytime when representing essentially the same data in different ways
          • example: physical units
            case class Meter(whole: Int, fraction: Int)
            case class Centimeter(whole: Int)
            
            val centimeterToMeterIso = Iso[Centimeter, Meter] { cm =>
              Meter(cm.whole / 100, cm.whole % 100)
            }{ m =>
              Centimeter(m.whole * 100 + m.fraction)
            }
            
            centimeterToMeterIso.modify(m => m.copy(m.whole + 3))(Centimeter(155))
            
      • Prism[S, A]
        • used for
          • sum type (coproduct)
            • example: sealed trait or Enum
          • when there is no 1-1 correspondence
            • example
              • String – Int
                • not all strings are transformable to Int, all Int transformable to String
                  • String <- Int
                  • String -> Option[Int]
                • digression
                  • notice that "0003".toInt == 3 but it is not reversible
                    • you have to put more constraint on String to have that example represents Prism
        • laws
          • if there is an answer, going back must give the source
          • if we go back, there must be an answer, which is the source
        • vs lens
          • sometimes fails to get target (Option) but you can always come back
      • Optional[S, A]
        • you may think of Optional as something more general than both Prism and Lens
          • Prism: the element A we are trying to focus may not exist
          • Lens: we don’t have enough information to go back to S without additional argument
          • summary: worst part of lens and prism – can fail to retrieve and can fail to get back
        • example
          • setup
            sealed trait Error
            
            case class ErrorA(message: String, details: String) extends Error
            case object ErrorB extends Error
            
          • task: Optional[Error, String] to “zoom into” details
            • cannot be Lens[Error, String] as ErrorB does not contain details
          • solution
            val getErrorADetails: Lens[ErrorA, String] = GenLens[ErrorA](_.details)
            val getErrorA: Prism[Error, ErrorA] = GenPrism[Error, ErrorA]
            val getDetails: Optional[Error, String] = getErrorA andThen getErrorADetails
            
        • it’s quite rare to see Optional implemented directly
          • usually: implement Prism and Lens and then compose them together
      • Traversal[S, A]
        • like optional that can focus on 0 to n fields
        • generalisation of an Optional to several targets
        • example: focus into all elements inside of a container (e.g. List, Vector, Option)
      • At[S, I, A]
        • an “index” I, provides a Lens[S, A]
        • built-in instances for map and set
        • Lens
      • Index[S, I, A]
        • like At
        • works on an optional index
        • built-in: list, map, string etc
        • Prism
        • can’t delete the value under the key
    • hierarchy (based on monocle)
      • diagram meant to be read as UML class hierarchy diagram
        • arrow going from Lens to Optional means that Lens is a special case of Optional
        • what does it mean that both Lens and Prism can be treated as Optional?
          • Lens is an Optional for which getOption always succeeds
          • Prism is an Optional for which we ignores S (“the whole part”)
            • A (“the specific part”) holds all information to produce new S
      • diagrams alt text alt text alt text

    monocle

    • composing
      val setStreetNumber = GenLens[Address](_.streetNumber)
      val setAddress = GenLens[User](_.address)
      val composed = setAddress andThen setStreetNumber
      
      composed.replace(newStreetNo)(user)
      
    • focus

    quicklens

    person
      .modify(_.address.street.name).using(_.toUpperCase)
      .modify(_.age).setTo(22)
      .modify(_.weight).setToIfDefined(Some(100))
      .modify(_.payments.each.result).using(_.toUpperCase)
      .modify(_.props.at("Height").value).setTo(178)
      .modify(_.pets.each.when[Dog].age).using(_ + 1)
    
    Visit original content creator repository https://github.com/mtumilowicz/scala-optics-monocle-quicklens-workshop
  • pacote

    Visit original content creator repository
    https://github.com/zkat/pacote

  • Project-NFC

    Project-NFC Development Report

    !!THIS SOFTWARE WAS PART OF A HIGH SCHOOL IN-CLASS ASSIGNMENT!!

    Image 1

    COVID-19 had a significant impact on our lives, which we still continue to suffer from. Tourism has been one of the most impacted industries around the world, and the tourism industry in Japan is no exception. However in 2021, many businesses have started to reopen and many other restrictions are expected to follow suit, thanks to the hard work of the front-line workers and the vaccines manufactures. With our lives returning to the normal, it has become the perfect time for people to start their planning for their next vacation. My software, Project NFC, encourages people around the world to visit Japan, by showcasing the beauties of Japan and its culture, while also delivering COVID related facts for a safer trip.

    Underlying Software Design

    The 2 factors, interests toward Japan and safeties of traveling, had to be accounted for in my software. Does are what the visitors are most concerned about. Therefore, my decision was to use simple maps and short articles derived from Wikipedia.org as the main features.

    I chose to use a single map instead of dozens of graphs, since many websites online take the graph approach, which I find to be challenging to comprehend at a single glance. While graphs are convenient in packing huge amounts of information, I wanted a solution that was both easy to understand and less space consuming. Having a single image that uses colored dots fulfills my expectations, rather than 47 graphs, one for each prefecture.

    Image above is a screenshot of the final product. The JPanel contains a map of Japan, with colored dots in the foreground to indicate the risks of visiting the prefecture. The risk is estimated from a value which is the average of the case numbers in the past 2 weeks divided by the population. If that value is above a certain threshold, the risk is determined to be moderate or high (0.00003, or average of 3 new cases in a population of 10,000 is considered high risk). By using colored dots, I have embraced the simplicity of the diagram, making the software easier for the user to understand. Even though I must note that those calculated “risk values” are lacking scientific evidence, I think it may be one of the factors that the users can consider in their trip planning process and also during their trip.

    The decision of deriving articles from Wikipedia.org has been made considering the accuracy of their information and the iterative development made by the community. Different approach may have been to write the articles by myself, which would have been much more time consuming and may also be limited in its credibility. Furthermore, another factor that lead to my decision is that many other APIs were paid or did not work web-based (Ex: requires the implementation of JavaScript). In contrast, API from Wikipedia.org is free to use, and is very responsive.

    Image 2

    This is another screenshot from the final product. On the left side, there are short articles that describe the place, along with a couple images to gain a better understanding. As I have said, the articles are derived from wikipedia.org, and the user can read more about the place by clicking on “(From Wikipedia)”. The other hyperlink, “See in Google map”, opens Google map in the browser. The map on the right side gives the user a brief understanding of where the place is located. The images are chosen by me, and were downloaded from HERE. The images were all free to use. I had to downsize or upscale some of the images to make them fit into the GUI.

    In behind the GUIs, I had been working hard on making the software as generic as possible. Since I plan on expanding on this software for not only Japan but also other countries, I used Java features such as inheritance and abstract classes so that I can reuse most of the classes from this program. I have also implemented an easy-injection feature for the language files, avoiding the hard-coding of texts for each language option. Currently, the program supports English and partially, Japanese.

    Conclusion

    As for the conclusion, I consider that my program and its main two features have been successful in addressing the proposed issue. By combining the ideas of case number dash board and a guidebook, it acts as a tool for the people around the world to know more about Japan, and to also consider about their next trip. Unfortunately, it is only a suggesting tool, and is only combining the features found online, so it has very limited amounts of originality. In future programs, I should combine other studies, such as business, in order to address the issue around the idea of market-in, and not product-in. Also, this program has its major problem in gaining more users, so cooperating with actual businesses and listening to the consumers will be my next goal. Nonetheless, I strongly wish that people who are planning their next vacation to stop by and take a peek at my software; and potentially, have a safe and exciting trip in Japan.

    Future Development

    • Improving the accuracy in risk prediction.
      • May be addressed by using machine learning and using a data model to give a prediction.
    • Reinforcing the translator section
      • Using paid API services such as Google Translate API is an option.
    • Expanding the platform to gain more users
      • Shifting development towards web-base and smartphone users can be a step forward.

    Huge thanks to our Computer Science teacher Mr. Cheng for his support, and also to my classmates.

    Special thanks to my parents, my sister, and my best friend, Nic. 🙂

    Visit original content creator repository https://github.com/Mirai7865/Project-NFC
  • WallhavenApi

    Wallhaven API for Python

    pipeline status Coverage Status codecov Scrutinizer Code Quality Dependency Status

    Feel free to add an issue.

    Description

    Implementation of https://wallhaven.cc/help/api (the official one)

    Dependencies

    # pip install -r requirements.txt

    Quick Documentation

    Import WallhavenApi package:

    import wallhavenapi

    Initialize WallhavenApi:

    wallhaven_api= wallhavenapi.WallhavenApiV1()

    If you have an account on Wallhaven, you can use api key to access all available wallpapers:

    wallhaven_api = wallhavenapi.WallhavenApiV1(api_key="some_api_key")

    Methods

    WallhavenApiV1

    • api_key {String} – your Wallhaven api key
    • verify_connection {Bool} – check ssl cert
    • base_url {String} – Wallhaven base api url
    • timeout {Tuple(Int, Int)} – http request timeout
    • requestslimit_timeout {Tuple(Int, Int)} – try to handle 429 http error. The first Int – how many times to do request, the second – delay (in seconds) between requests.

    WallhavenApiV1.search – Accessing Wallpaper information

    • q {String} – query (used to filter by user, tags, ids and so on…)
    • categories {String|List[String]} – walpaper category (located in wallhavenapi.Category)
    • purities {String|List[String]} – walpaper purity (located in wallhavenapi.Purity)
    • sorting {String} – how to sort results (located in wallhavenapi.Sorting)
    • order {String} – sort order (located in wallhavenapi.Order)
    • top_range {String} – sorting MUST be set to ‘toplist’ (located in wallhavenapi.TopRange)
    • atleast {Typle(Int,Int)} – minimum resolution
    • resolutions {Typle(Int,Int)|List[Typle(Int,Int)]} – exact wallpaper resolutions
    • ratios {Typle(Int,Int)|List[Typle(Int,Int)]} – aspect ratios
    • colors {String} – color to search (located in wallhavenapi.Color)
    • page {Int} – page for pagination

    WallhavenApiV1.is_walpaper_exists – Check wallpaper existence by id

    • wallpaper_id {String} – wallpaper id (can be obtained by WallhavenApiV1.search)

    WallhavenApiV1.download_wallpaper – Download wallpaper to file (or get data) by id

    • wallpaper_id {String} – wallpaper id (WallhavenApiV1.search)
    • file_path {String} – path to file
    • chunk_size {Int} – chunked buffer for downloading

    WallhavenApiV1.tag – Get tag information

    • tag_id {String} – tag id (can be obtained by WallhavenApiV1.search)

    WallhavenApiV1.settings – Get user settings

    It works only if api_key is set

    WallhavenApiV1.collections – get collections by user name

    • user_name {String} – user name

    WallhavenApiV1.collection_wallpapers – get wallpapers by user collection id

    • user_name {String} – user name
    • collection_id {Int} – user collection id (can obtained by WallhavenApiV1.collections)
    • page {Int} – page

    WallhavenApiV1.my_collections – get collections of api_key user

    It works only if api_key is set

    Visit original content creator repository https://github.com/Goblenus/WallhavenApi
  • lime

    Mighty-Seagull

    Lime

    A Fresh Cascading style sheets Framework.

    Table of contents

    Description

    A CSS & SCSS Framework with both flex and classic 12 column grid system for responsive web sites and applications.

    Installation

    You can use Lime with two ways. Use the CSS file or the SASS files. Clone the Mighty Seagull repo:

    $ git clone https://github.com/thebigbaron/lime.git
    

    CSS

    Use the dist/lime/lime.css version or dist/lime/lime.min.css minified one by placing one of the following in your html head section.

    <link rel="stylesheet" href="https://github.com/thebigbaron/dist/lime/lime.css">
    

    or

    <link rel="stylesheet" href="https://github.com/thebigbaron/dist/lime/lime.min.css">
    

    Note: Be carefull with the path of the file

    SCSS

    Copy the lime folder from src.

    Paste it inside your SCSS folder.

    Import it in your main scss file:

    @import "lime";
    

    Documentation

    You can find documentantion and examples in the Lime site. Documentation w/ examples

    Examples

    You can find examples in the link provided in the documentation section.

    Contributing

    When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository before making a change.

    You may contribute in several ways like creating new features, fixing bugs, improving documentation and examples.

    Issue Template

    Bug report


    name: Bug report

    about: Create a report to help us improve

    title:

    labels:

    assignees:


    Describe the bug A clear and concise description of what the bug is.

    To Reproduce Steps to reproduce the behavior:

    1. Go to ‘…’
    2. Click on ‘….’
    3. Scroll down to ‘….’
    4. See error

    Expected behavior A clear and concise description of what you expected to happen.

    Screenshots If applicable, add screenshots to help explain your problem.

    Desktop (please complete the following information):

    • OS: [e.g. MACOS]
    • Browser [e.g. chrome, safari]
    • Version of Lime

    Additional context Add any other context about the problem here.

    Feature request


    name: Feature request

    about: Suggest an idea for this project

    title:

    labels:

    assignees:


    Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I’m always frustrated when […]

    Describe the solution you’d like A clear and concise description of what you want to happen.

    Describe alternatives you’ve considered A clear and concise description of any alternative solutions or features you’ve considered.

    Additional context Add any other context or screenshots about the feature request here.

    Pull Requests

    Description of the Change

    We must be able to understand the design of your change from this description. If we can’t get a good idea of what the code will be doing from the description here, the pull request may be closed at the maintainers’ discretion. Keep in mind that the maintainer reviewing this PR may not be familiar with or have worked with the code here recently, so please walk us through the concepts.

    Alternate Designs

    Explain what other alternates were considered and why the proposed version was selected

    Benefits

    What benefits will be realized by the code change?

    Possible Drawbacks

    What are the possible side-effects or negative impacts of the code change?

    Membership

    New members are welcome if they have the same philosophy with the organization.

    License

    MIT – Free Software for Everyone.

    Visit original content creator repository https://github.com/thebigbaron/lime
  • lime

    Mighty-Seagull

    Lime

    A Fresh Cascading style sheets Framework.

    Table of contents

    Description

    A CSS & SCSS Framework with both flex and classic 12 column grid system for responsive web sites and applications.

    Installation

    You can use Lime with two ways. Use the CSS file or the SASS files. Clone the Mighty Seagull repo:

    $ git clone https://github.com/thebigbaron/lime.git
    

    CSS

    Use the dist/lime/lime.css version or dist/lime/lime.min.css minified one by placing one of the following in your html head section.

    <link rel="stylesheet" href="https://github.com/thebigbaron/dist/lime/lime.css">
    

    or

    <link rel="stylesheet" href="https://github.com/thebigbaron/dist/lime/lime.min.css">
    

    Note: Be carefull with the path of the file

    SCSS

    Copy the lime folder from src.

    Paste it inside your SCSS folder.

    Import it in your main scss file:

    @import "lime";
    

    Documentation

    You can find documentantion and examples in the Lime site. Documentation w/ examples

    Examples

    You can find examples in the link provided in the documentation section.

    Contributing

    When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository before making a change.

    You may contribute in several ways like creating new features, fixing bugs, improving documentation and examples.

    Issue Template

    Bug report


    name: Bug report

    about: Create a report to help us improve

    title:

    labels:

    assignees:


    Describe the bug A clear and concise description of what the bug is.

    To Reproduce Steps to reproduce the behavior:

    1. Go to ‘…’
    2. Click on ‘….’
    3. Scroll down to ‘….’
    4. See error

    Expected behavior A clear and concise description of what you expected to happen.

    Screenshots If applicable, add screenshots to help explain your problem.

    Desktop (please complete the following information):

    • OS: [e.g. MACOS]
    • Browser [e.g. chrome, safari]
    • Version of Lime

    Additional context Add any other context about the problem here.

    Feature request


    name: Feature request

    about: Suggest an idea for this project

    title:

    labels:

    assignees:


    Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I’m always frustrated when […]

    Describe the solution you’d like A clear and concise description of what you want to happen.

    Describe alternatives you’ve considered A clear and concise description of any alternative solutions or features you’ve considered.

    Additional context Add any other context or screenshots about the feature request here.

    Pull Requests

    Description of the Change

    We must be able to understand the design of your change from this description. If we can’t get a good idea of what the code will be doing from the description here, the pull request may be closed at the maintainers’ discretion. Keep in mind that the maintainer reviewing this PR may not be familiar with or have worked with the code here recently, so please walk us through the concepts.

    Alternate Designs

    Explain what other alternates were considered and why the proposed version was selected

    Benefits

    What benefits will be realized by the code change?

    Possible Drawbacks

    What are the possible side-effects or negative impacts of the code change?

    Membership

    New members are welcome if they have the same philosophy with the organization.

    License

    MIT – Free Software for Everyone.

    Visit original content creator repository https://github.com/thebigbaron/lime
  • plp-mtu-discovery

    plp-mtu-discovery

    Perform Path MTU Discovery without relying on ICMP errors, which are often not delivered.

    This program performs Packetization Layer Path MTU Discovery as described in RFC 4821, which is a more reliable way to detect MTU size in presence of ICMP black holes.

    Rationale

    While TCP connections automatically adjust MTU size over time depending on various indicators (network performance, packet loss, ICMP error messages, …), this is not the case for connection-less protocols.

    When performance is essential, throughput, packet fragmentation and route reliability are three key indicators to analyze in order to optimize stream performance. Since route reliability does not always depend on us, we should strive to maximize throughput while NOT performing packet fragmentation, which can severely degrade performance[1].

    The original proposal for Path MTU Discovery relied on ICMP Fragmentation Needed packets to be delivered when a IPv4 packet with Don’t Fragment field set was too large to be propagated. Unfortunately, some routers do not generate these kind of errors but choose to silently ignore large packets instead. A client has no way to determine the cause of the packet loss.

    A safer approach

    Since all hosts are mandated to support ICMP_ECHO queries, we can exploit the fact that ICMP messages accept an arbitrary amount of data and send different-sized packets to our server. If we turn on the Don’t Fragment field in the IPv4 packet and listen for a response, we are de facto waiting for an ACK (in the form of an ICMP_ECHOREPLY packet) confirming that this MTU size is valid.

    Now we just have to perform a binary search in respect to the size of the packets in order to find the maximum MTU size supported by this route.

    ICMP mode

    When in ICMP mode, some ICMP_ECHO requests of different sizes are generated.

    • If a response from the server is received, that MTU size is considered valid and the threshold is raised.
    • If no response is received after several attempts or some kind of error is received (e.g.ICMP Fragmentation Needed), that MTU size is declared invalid and the threshold is lowered.

    The only requirement of ICMP mode is that the host must be capable to reply to ping messages.

    UDP mode

    The same algorithm applies to UDP packets, but you need to run a server (udp_server.py) on your receiving host in order to send back acknowledgment messages.

    Compiling & Running

    This program should run fine on most Linux distributions and OSX.

    gcc -Wall -Wextra mtu_discovery.c mtu.c -o plpmtu
    

    It should not report warnings/errors. If it does, please open an issue.

    If you want to run in ICMP mode type:

    sudo ./plpmtu -p icmp -s <server-ipaddr>
    

    If you want to run UDP mode instead:

    sudo ./plpmtu -p udp -s <server-ipaddr:port>
    

    Admin rights are required in order to use raw sockets.

    Command line arguments

    Specifier Description
    -p {icmp/udp} Select in which mode to operate.
    -s <addr[:port]> Specify server’s address. If running in UDP mode, you must also specify the destination port by appending ‘:port’ (e.g. -s 8.8.8.8:12345)
    -l <addr:port> Optional. Select on which address to bind(); used in UDP mode; might be removed.
    -t <timeout> Optional. Select the maximum time to wait for a response from the server, default is 1 second; time is expressed in milliseconds.
    -r <max-reqs> Optional. Select the maximum number of failed attempts needed to declare a MTU size invalid, default is 3 attempts.

    Examples

    sudo ./plpmtu -p icmp -s 184.12.26.131
    

    Perform MTU discovery (ICMP mode) with 184.12.26.131.

    sudo ./plpmtu -p udp -s 184.12.26.131:24000 -t 1500 -r 5
    

    Perform MTU discovery (UDP mode) with 184.12.26.131 on port 24000. If a response is not received within 1.5 seconds for 5 times in a row, reduce MTU threshold.

    Docs

    Visit original content creator repository
    https://github.com/Kavarenshko/plp-mtu-discovery

  • sqltomango

    sqltomango

    Build Status npm version

    A simple Node.js library that converts Structured Query Language (SQL) into CouchDB Mango / Cloudant Query JSON objects. Mango is the code name for the query language used in Apache CouchDB and IBM Cloudant. It uses JSON to represent queries. This tool converts SQL strings into Mango objects, to allow users to interact with CouchDB/Cloudant database with SQL queries.

    Installation

    Use sqltomango in your project with:

    npm install --save sqltomango

    Import the library into your code:

    var sqltomango = require('sqltomango');

    Usage

    The sqltomango library is a single function that accepts one argument – a string containing the SQL you wish to convert to a query object:

    var q = sqltomango.parse("SELECT * FROM dogs WHERE owner = 'glynn'")

    It returns an object which can be used to to query a CouchDB or Cloudant database:

    {
      "table": "dogs",
      "selector": {
        "owner": {
        "$eq": "glynn"
        }
      }
    }

    This works for more complex queries too:

    var q = sqltomango.parse("SELECT _id, age, breed FROM dogs WHERE owner = 'glynn' OR (name='towser' AND colour='white') ORDER BY age DESC LIMIT 500,1500")
    // produces...
    {
      "table": "dogs",
      "fields": [
        "_id",
        "age",
        "breed"
      ],
      "selector": {
        "$or": [
          {
            "owner": {
              "$eq": "glynn"
            }
          },
          {
            "$and": [
              {
                "name": {
                  "$eq": "towser"
                }
              },
              {
                "colour": {
                  "$eq": "white"
                }
              }
            ]
          }
        ]
      },
      "sort": [
        {
          "age": "desc"
        }
      ],
      "limit": 1500,
      "skip": 500
    }

    Errors

    An exception is thrown if the SQL does not parse or contains SQL features not supported by Mango including:

    • GROUP BY
    • SUM/COUNT/AVG/DISTINCT
    • UNION
    • JOIN
    Visit original content creator repository https://github.com/glynnbird/sqltomango
  • react-native-picker-box

    react-native-picker-box

    [This package has been deprecated]
    Package no longer supported. Use at your own risk.


    Simple and configurable component picker for react native

    Table of contents

    Usage

    Installation

    Just add react-native-picker-box to your project:

    yarn add react-native-picker-box

    Basic example

    ANDROID IOS
    Basic example gif android Basic example gif ios

    Import

    import PickerBox from 'react-native-picker-box';

    Using a react-native-picker-box

    export default class App extends Component {
    
      state={
        data: [
          {label: 'Português', value: 'pt'},
          {label: 'Deutsch', value: 'de'},
          {label: 'English', value: 'en'}
        ],
        selectedValue: ''
      }
    
      render() {
        return (
          <View style={styles.container}>
            <Text style={styles.welcome} onPress={() => this.myref.openPicker() }>Press to select language</Text>
            <Text style={styles.instructions}>{ this.state.selectedValue }</Text>
            <PickerBox
              ref={ref => this.myref = ref}
              data={ this.state.data }
              onValueChange={value => this.setState({ selectedValue: value })}
              selectedValue={ this.state.selectedValue }
            />
          </View>
        );
      }
    }

    Props and Methods

    Props

    Name Type Default Required Description
    data array none Yes Each item should be in the following format: {label: 'JavaScript', value: 'js'}
    onValueChange function null Yes Callback for when an item is selected. This is called with the following parameter: value
    selectedValue string none No Value matching value of one of the items. Can be a string or an integer.
    maxHeight number - No Custom maxHeight. Is the maximum height for this component.
    statusbar boolean true No StatusBar overlapping.
    itemTextColor string #757379 No Custom item text color.
    separatorColor string #757379 No Custom separator color.
    prevTextColor string #572580 No Custom button(prev) text color.
    prevTextLabel string Cancel No Custom button(prev) text label.

    Methods

    Method Name Arguments Description
    openPicker null Open picker. Use refs for open Picker [following the example]

    Contributing

    Thanks for being interested on making this package better.

    git clone https://github.com/duhwcarvalho/react-native-picker-box.git
    cd react-native-picker-box
    react-native upgrade
    yarn
    react-native run-android or react-native run-ios

    Author


    @duhwcarvalho
    Visit original content creator repository https://github.com/duhwcarvalho/react-native-picker-box
  • react-native-picker-box

    react-native-picker-box

    [This package has been deprecated]
    Package no longer supported. Use at your own risk.


    Simple and configurable component picker for react native

    Table of contents

    Usage

    Installation

    Just add react-native-picker-box to your project:

    yarn add react-native-picker-box

    Basic example

    ANDROID IOS
    Basic example gif android Basic example gif ios

    Import

    import PickerBox from 'react-native-picker-box';

    Using a react-native-picker-box

    export default class App extends Component {
    
      state={
        data: [
          {label: 'Português', value: 'pt'},
          {label: 'Deutsch', value: 'de'},
          {label: 'English', value: 'en'}
        ],
        selectedValue: ''
      }
    
      render() {
        return (
          <View style={styles.container}>
            <Text style={styles.welcome} onPress={() => this.myref.openPicker() }>Press to select language</Text>
            <Text style={styles.instructions}>{ this.state.selectedValue }</Text>
            <PickerBox
              ref={ref => this.myref = ref}
              data={ this.state.data }
              onValueChange={value => this.setState({ selectedValue: value })}
              selectedValue={ this.state.selectedValue }
            />
          </View>
        );
      }
    }

    Props and Methods

    Props

    Name Type Default Required Description
    data array none Yes Each item should be in the following format: {label: 'JavaScript', value: 'js'}
    onValueChange function null Yes Callback for when an item is selected. This is called with the following parameter: value
    selectedValue string none No Value matching value of one of the items. Can be a string or an integer.
    maxHeight number - No Custom maxHeight. Is the maximum height for this component.
    statusbar boolean true No StatusBar overlapping.
    itemTextColor string #757379 No Custom item text color.
    separatorColor string #757379 No Custom separator color.
    prevTextColor string #572580 No Custom button(prev) text color.
    prevTextLabel string Cancel No Custom button(prev) text label.

    Methods

    Method Name Arguments Description
    openPicker null Open picker. Use refs for open Picker [following the example]

    Contributing

    Thanks for being interested on making this package better.

    git clone https://github.com/duhwcarvalho/react-native-picker-box.git
    cd react-native-picker-box
    react-native upgrade
    yarn
    react-native run-android or react-native run-ios

    Author


    @duhwcarvalho
    Visit original content creator repository https://github.com/duhwcarvalho/react-native-picker-box